<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>RBAC权限管理 | 浪飞IT小栈</title>
    <meta name="generator" content="VuePress 1.9.9">
    <link rel="icon" href="/img/favicon.ico">
    <meta name="description" content="前后端技术分享">
    <meta name="keywords" content="前端博客,后端博客,人工智能,个人技术博客,前端,前端开发,前端框架,web前端,前端面试题,技术文档,学习,面试,JavaScript,js,ES6,TypeScript,vue,python,css3,html5,Node,git,github,markdown,java,Spring,Springboot,Redis,mysql">
    <meta name="baidu-site-verification" content="7F55weZDDc">
    <meta name="theme-color" content="#11a8cd">
    
    <link rel="preload" href="/assets/css/0.styles.ada71c49.css" as="style"><link rel="preload" href="/assets/js/app.a2a00aa5.js" as="script"><link rel="preload" href="/assets/js/2.256f807a.js" as="script"><link rel="preload" href="/assets/js/58.932e3864.js" as="script"><link rel="prefetch" href="/assets/js/10.ab144fe3.js"><link rel="prefetch" href="/assets/js/11.062e0879.js"><link rel="prefetch" href="/assets/js/12.f9c60882.js"><link rel="prefetch" href="/assets/js/13.0a6ebfcf.js"><link rel="prefetch" href="/assets/js/14.03fc6f12.js"><link rel="prefetch" href="/assets/js/15.19dbf9fd.js"><link rel="prefetch" href="/assets/js/16.6e68160b.js"><link rel="prefetch" href="/assets/js/17.1d678834.js"><link rel="prefetch" href="/assets/js/18.4536fd5c.js"><link rel="prefetch" href="/assets/js/19.9c1b65df.js"><link rel="prefetch" href="/assets/js/20.c5f96e71.js"><link rel="prefetch" href="/assets/js/21.3f0b334e.js"><link rel="prefetch" href="/assets/js/22.55c868be.js"><link rel="prefetch" href="/assets/js/23.91286241.js"><link rel="prefetch" href="/assets/js/24.c1af1909.js"><link rel="prefetch" href="/assets/js/25.a995a755.js"><link rel="prefetch" href="/assets/js/26.abc5dea3.js"><link rel="prefetch" href="/assets/js/27.1eb62850.js"><link rel="prefetch" href="/assets/js/28.b383a871.js"><link rel="prefetch" href="/assets/js/29.088e0d73.js"><link rel="prefetch" href="/assets/js/3.cd716564.js"><link rel="prefetch" href="/assets/js/30.3ccc98d5.js"><link rel="prefetch" href="/assets/js/31.aab7cc23.js"><link rel="prefetch" href="/assets/js/32.5e16b7e6.js"><link rel="prefetch" href="/assets/js/33.4aaeb741.js"><link rel="prefetch" href="/assets/js/34.45e8e898.js"><link rel="prefetch" href="/assets/js/35.5899c97a.js"><link rel="prefetch" href="/assets/js/36.32e2e263.js"><link rel="prefetch" href="/assets/js/37.c4e27bbd.js"><link rel="prefetch" href="/assets/js/38.fe5e60d2.js"><link rel="prefetch" href="/assets/js/39.2cb0b4ee.js"><link rel="prefetch" href="/assets/js/4.edf4d5e7.js"><link rel="prefetch" href="/assets/js/40.08d5bead.js"><link rel="prefetch" href="/assets/js/41.2f80f24b.js"><link rel="prefetch" href="/assets/js/42.43a49913.js"><link rel="prefetch" href="/assets/js/43.08da7ec2.js"><link rel="prefetch" href="/assets/js/44.7c7cb8a9.js"><link rel="prefetch" href="/assets/js/45.0ddf7e7f.js"><link rel="prefetch" href="/assets/js/46.7c4609a5.js"><link rel="prefetch" href="/assets/js/47.edf3ddf8.js"><link rel="prefetch" href="/assets/js/48.569a90b9.js"><link rel="prefetch" href="/assets/js/49.fa368ac8.js"><link rel="prefetch" href="/assets/js/5.24054156.js"><link rel="prefetch" href="/assets/js/50.b9b3ea7f.js"><link rel="prefetch" href="/assets/js/51.a2ebf0fe.js"><link rel="prefetch" href="/assets/js/52.31f02238.js"><link rel="prefetch" href="/assets/js/53.4110ef1e.js"><link rel="prefetch" href="/assets/js/54.dab741c7.js"><link rel="prefetch" href="/assets/js/55.f27984e9.js"><link rel="prefetch" href="/assets/js/56.bd4c1a8e.js"><link rel="prefetch" href="/assets/js/57.375d40cb.js"><link rel="prefetch" href="/assets/js/59.e657be1b.js"><link rel="prefetch" href="/assets/js/6.b259c061.js"><link rel="prefetch" href="/assets/js/60.88d94274.js"><link rel="prefetch" href="/assets/js/61.fa71d884.js"><link rel="prefetch" href="/assets/js/62.a9d3b3b7.js"><link rel="prefetch" href="/assets/js/63.f849fe75.js"><link rel="prefetch" href="/assets/js/64.1770f7a9.js"><link rel="prefetch" href="/assets/js/65.6afc33db.js"><link rel="prefetch" href="/assets/js/66.799f2eb4.js"><link rel="prefetch" href="/assets/js/67.f36b2f7d.js"><link rel="prefetch" href="/assets/js/68.868cd00b.js"><link rel="prefetch" href="/assets/js/69.4c29b907.js"><link rel="prefetch" href="/assets/js/7.e34ea00f.js"><link rel="prefetch" href="/assets/js/70.1b993ed4.js"><link rel="prefetch" href="/assets/js/71.e16ad4f9.js"><link rel="prefetch" href="/assets/js/72.627505e6.js"><link rel="prefetch" href="/assets/js/73.32768e14.js"><link rel="prefetch" href="/assets/js/74.968a580d.js"><link rel="prefetch" href="/assets/js/75.42d5af3d.js"><link rel="prefetch" href="/assets/js/76.19a69d31.js"><link rel="prefetch" href="/assets/js/77.cf627a78.js"><link rel="prefetch" href="/assets/js/78.07177880.js"><link rel="prefetch" href="/assets/js/8.c4dd00ce.js"><link rel="prefetch" href="/assets/js/9.0ff73be2.js">
    <link rel="stylesheet" href="/assets/css/0.styles.ada71c49.css">
  </head>
  <body class="theme-mode-light">
    <div id="app" data-server-rendered="true"><div class="theme-container sidebar-open have-rightmenu"><header class="navbar blur"><div title="目录" class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/" class="home-link router-link-active"><img src="/img/logo.png" alt="浪飞IT小栈" class="logo"> <span class="site-name can-hide">浪飞IT小栈</span></a> <div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/" class="nav-link">首页</a></div><div class="nav-item"><a href="/pages/6138ae/" class="nav-link">Java速通</a></div><div class="nav-item"><a href="/pages/705b35/" class="nav-link">面试经典</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="精品项目" class="dropdown-title"><a href="/project/" class="link-title">精品项目</a> <span class="title" style="display:none;">精品项目</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><h4>桌面端</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/pages/8143cc480faf9a11/" class="nav-link">JavaScript</a></li></ul></li><li class="dropdown-item"><h4>PC后端</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/note/javascript/" class="nav-link">项目1</a></li><li class="dropdown-subitem"><a href="/note/typescript-axios/" class="nav-link">项目二</a></li></ul></li></ul></div></div><div class="nav-item"><a href="/about/" class="nav-link">电子书</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="索引" class="dropdown-title"><a href="/archives/" class="link-title">索引</a> <span class="title" style="display:none;">索引</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/categories/" class="nav-link">分类</a></li><li class="dropdown-item"><!----> <a href="/tags/" class="nav-link">标签</a></li><li class="dropdown-item"><!----> <a href="/archives/" class="nav-link">归档</a></li></ul></div></div><div class="nav-item"><a href="/pages/0c07b2/" class="nav-link">IT杂货铺</a></div><div class="nav-item"><a href="/archives/" class="nav-link">更多资源</a></div> <!----></nav></div></header> <div class="sidebar-mask"></div> <div class="sidebar-hover-trigger"></div> <aside class="sidebar" style="display:none;"><div class="blogger"><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/avatar.jpg"> <div class="blogger-info"><h3>浪飞yes</h3> <span>无法简单的人儿~</span></div></div> <nav class="nav-links"><div class="nav-item"><a href="/" class="nav-link">首页</a></div><div class="nav-item"><a href="/pages/6138ae/" class="nav-link">Java速通</a></div><div class="nav-item"><a href="/pages/705b35/" class="nav-link">面试经典</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="精品项目" class="dropdown-title"><a href="/project/" class="link-title">精品项目</a> <span class="title" style="display:none;">精品项目</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><h4>桌面端</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/pages/8143cc480faf9a11/" class="nav-link">JavaScript</a></li></ul></li><li class="dropdown-item"><h4>PC后端</h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/note/javascript/" class="nav-link">项目1</a></li><li class="dropdown-subitem"><a href="/note/typescript-axios/" class="nav-link">项目二</a></li></ul></li></ul></div></div><div class="nav-item"><a href="/about/" class="nav-link">电子书</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="索引" class="dropdown-title"><a href="/archives/" class="link-title">索引</a> <span class="title" style="display:none;">索引</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/categories/" class="nav-link">分类</a></li><li class="dropdown-item"><!----> <a href="/tags/" class="nav-link">标签</a></li><li class="dropdown-item"><!----> <a href="/archives/" class="nav-link">归档</a></li></ul></div></div><div class="nav-item"><a href="/pages/0c07b2/" class="nav-link">IT杂货铺</a></div><div class="nav-item"><a href="/archives/" class="nav-link">更多资源</a></div> <!----></nav>  <ul class="sidebar-links"><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>开篇</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>基础语法</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>面向对象</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>常用类</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>基础高级</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>数据库</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>前端技术</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>热门框架</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>前后端分离</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading open"><span>权限控制</span> <span class="arrow down"></span></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/pages/7d8299/" aria-current="page" class="active sidebar-link">RBAC权限管理</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level2"><a href="/pages/7d8299/#一、今日学习内容与目标" class="sidebar-link">一、今日学习内容与目标</a></li><li class="sidebar-sub-header level2"><a href="/pages/7d8299/#二、权限控制概述" class="sidebar-link">二、权限控制概述</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level3"><a href="/pages/7d8299/#_2-1-访问控制目的" class="sidebar-link">2.1 访问控制目的</a></li><li class="sidebar-sub-header level3"><a href="/pages/7d8299/#_2-2-访问控制策略" class="sidebar-link">2.2 访问控制策略</a></li><li class="sidebar-sub-header level3"><a href="/pages/7d8299/#_2-3-rbac-重要对象" class="sidebar-link">2.3 RBAC 重要对象</a></li><li class="sidebar-sub-header level3"><a href="/pages/7d8299/#_2-4-rbac-流程与模型分析" class="sidebar-link">2.4 RBAC 流程与模型分析</a></li><li class="sidebar-sub-header level3"><a href="/pages/7d8299/#_2-5-项目演示" class="sidebar-link">2.5 项目演示</a></li></ul></li><li class="sidebar-sub-header level2"><a href="/pages/7d8299/#三、搭建项目" class="sidebar-link">三、搭建项目</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level3"><a href="/pages/7d8299/#_3-1-技术选型" class="sidebar-link">3.1 技术选型</a></li><li class="sidebar-sub-header level3"><a href="/pages/7d8299/#_3-2-导入依赖" class="sidebar-link">3.2 导入依赖</a></li><li class="sidebar-sub-header level3"><a href="/pages/7d8299/#_3-3-加入配置" class="sidebar-link">3.3 加入配置</a></li><li class="sidebar-sub-header level3"><a href="/pages/7d8299/#_3-4-创建启动类" class="sidebar-link">3.4 创建启动类</a></li><li class="sidebar-sub-header level3"><a href="/pages/7d8299/#_3-5-响应类定义" class="sidebar-link">3.5 响应类定义</a></li></ul></li><li class="sidebar-sub-header level2"><a href="/pages/7d8299/#四、部门管理" class="sidebar-link">四、部门管理</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level3"><a href="/pages/7d8299/#_4-1-部门列表" class="sidebar-link">4.1 部门列表</a></li><li class="sidebar-sub-header level3"><a href="/pages/7d8299/#_4-2-部门删除" class="sidebar-link">4.2 部门删除</a></li><li class="sidebar-sub-header level3"><a href="/pages/7d8299/#_4-3-部门新增" class="sidebar-link">4.3 部门新增</a></li><li class="sidebar-sub-header level3"><a href="/pages/7d8299/#_4-4-部门更新" class="sidebar-link">4.4 部门更新</a></li><li class="sidebar-sub-header level3"><a href="/pages/7d8299/#_4-5-部门集合" class="sidebar-link">4.5 部门集合</a></li><li class="sidebar-sub-header level3"><a href="/pages/7d8299/#_4-6-接口统一异常处理" class="sidebar-link">4.6 接口统一异常处理</a></li></ul></li><li class="sidebar-sub-header level2"><a href="/pages/7d8299/#五、权限管理" class="sidebar-link">五、权限管理</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level3"><a href="/pages/7d8299/#_5-1-权限列表" class="sidebar-link">5.1 权限列表</a></li><li class="sidebar-sub-header level3"><a href="/pages/7d8299/#_5-2-加载权限" class="sidebar-link">5.2 加载权限</a></li><li class="sidebar-sub-header level3"><a href="/pages/7d8299/#_5-3-权限集合" class="sidebar-link">5.3 权限集合</a></li></ul></li><li class="sidebar-sub-header level2"><a href="/pages/7d8299/#六、角色管理" class="sidebar-link">六、角色管理</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level3"><a href="/pages/7d8299/#_6-1-角色列表" class="sidebar-link">6.1 角色列表</a></li><li class="sidebar-sub-header level3"><a href="/pages/7d8299/#_6-2-角色新增" class="sidebar-link">6.2 角色新增</a></li><li class="sidebar-sub-header level3"><a href="/pages/7d8299/#_6-3-角色更新" class="sidebar-link">6.3 角色更新</a></li><li class="sidebar-sub-header level3"><a href="/pages/7d8299/#_6-4-角色删除" class="sidebar-link">6.4 角色删除</a></li><li class="sidebar-sub-header level3"><a href="/pages/7d8299/#_6-5-角色集合" class="sidebar-link">6.5 角色集合</a></li></ul></li><li class="sidebar-sub-header level2"><a href="/pages/7d8299/#七、员工管理" class="sidebar-link">七、员工管理</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level3"><a href="/pages/7d8299/#_7-1-员工列表" class="sidebar-link">7.1 员工列表</a></li><li class="sidebar-sub-header level3"><a href="/pages/7d8299/#_7-2-员工新增" class="sidebar-link">7.2 员工新增</a></li><li class="sidebar-sub-header level3"><a href="/pages/7d8299/#_7-3-员工更新" class="sidebar-link">7.3 员工更新</a></li><li class="sidebar-sub-header level3"><a href="/pages/7d8299/#_7-4-员工删除" class="sidebar-link">7.4 员工删除</a></li><li class="sidebar-sub-header level3"><a href="/pages/7d8299/#_7-5-更新管理员状态" class="sidebar-link">7.5 更新管理员状态</a></li><li class="sidebar-sub-header level3"><a href="/pages/7d8299/#_7-6-角色删除-维护关系" class="sidebar-link">7.6 角色删除(维护关系)</a></li></ul></li><li class="sidebar-sub-header level2"><a href="/pages/7d8299/#八、认证管理" class="sidebar-link">八、认证管理</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level3"><a href="/pages/7d8299/#_8-1-验证码获取" class="sidebar-link">8.1 验证码获取</a></li><li class="sidebar-sub-header level3"><a href="/pages/7d8299/#_8-2-登录功能" class="sidebar-link">8.2 登录功能</a></li><li class="sidebar-sub-header level3"><a href="/pages/7d8299/#_8-3-登出功能" class="sidebar-link">8.3 登出功能</a></li></ul></li><li class="sidebar-sub-header level2"><a href="/pages/7d8299/#九、拦截管理" class="sidebar-link">九、拦截管理</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level3"><a href="/pages/7d8299/#_9-1-登录拦截" class="sidebar-link">9.1 登录拦截</a></li><li class="sidebar-sub-header level3"><a href="/pages/7d8299/#_9-2-权限拦截" class="sidebar-link">9.2 权限拦截</a></li></ul></li><li class="sidebar-sub-header level2"><a href="/pages/7d8299/#十、项目联调" class="sidebar-link">十、项目联调</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header level3"><a href="/pages/7d8299/#_10-1-跨域全局配置" class="sidebar-link">10.1 跨域全局配置</a></li></ul></li><li class="sidebar-sub-header level2"><a href="/pages/7d8299/#十一、阅读前端代码" class="sidebar-link">十一、阅读前端代码</a></li><li class="sidebar-sub-header level2"><a href="/pages/7d8299/#十二、小结与作业" class="sidebar-link">十二、小结与作业</a></li></ul></li><li><a href="/pages/9927d2/" class="sidebar-link">SpringSecurity</a></li></ul></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>NoSQL</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>微服务</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>消息中间件</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>脚手架</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>技术增值</span> <span class="arrow right"></span></p> <!----></section></li></ul> </aside> <div><main class="page"><div class="theme-vdoing-wrapper "><div class="articleInfo-wrap" data-v-06225672><div class="articleInfo" data-v-06225672><ul class="breadcrumbs" data-v-06225672><li data-v-06225672><a href="/" title="首页" class="iconfont icon-home router-link-active" data-v-06225672></a></li> <li data-v-06225672><a href="/categories/?category=Java%E9%80%9F%E9%80%9A" title="分类" data-v-06225672>Java速通</a></li><li data-v-06225672><a href="/categories/?category=%E6%9D%83%E9%99%90%E6%8E%A7%E5%88%B6" title="分类" data-v-06225672>权限控制</a></li></ul> <div class="info" data-v-06225672><div title="作者" class="author iconfont icon-touxiang" data-v-06225672><a href="https://github.com/langfeiyes" target="_blank" title="作者" class="beLink" data-v-06225672>langfeiyes</a></div> <div title="创建时间" class="date iconfont icon-riqi" data-v-06225672><a href="javascript:;" data-v-06225672>2024-03-11</a></div> <!----></div></div></div> <!----> <div class="content-wrapper"><div class="right-menu-wrapper"><div class="right-menu-margin"><div class="right-menu-title">目录</div> <div class="right-menu-content"></div></div></div> <h1><img src="">RBAC权限管理<!----></h1>  <div class="theme-vdoing-content content__default"><h1 id="rbac权限管理"><a href="#rbac权限管理" class="header-anchor">#</a> RBAC权限管理</h1> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20240311185938402.png" alt=""></p> <h2 id="一、今日学习内容与目标"><a href="#一、今日学习内容与目标" class="header-anchor">#</a> 一、今日学习内容与目标</h2> <ul><li>了解权限管理系统</li> <li>设计并实现RBAC权限管理系统</li> <li>掌握自定义注解使用</li> <li>掌握SpringMVC拦截器使用</li> <li>掌握认证与授权实现</li></ul> <h2 id="二、权限控制概述"><a href="#二、权限控制概述" class="header-anchor">#</a> 二、权限控制概述</h2> <h3 id="_2-1-访问控制目的"><a href="#_2-1-访问控制目的" class="header-anchor">#</a> 2.1 访问控制目的</h3> <p>在实际的组织中，为了完成组织的业务工作，需要在组织内部设置不同的职位，职位既表示一种业务分工，又表示一种责任与权利。根据业务分工的需要，职位被划分给不同群体，各个群体的人根据其工作任务的需要被赋予不同的职责和权利，每个人有权了解与使用与自己任务相关的信息与资源，对于那些不应该被知道的信息则应该限制他们访问。这就产生了访问控制的需求。</p> <p>限制主体对资源的访问，限制用户可以访问而且只能访问自己被授权的资源，从而保障数据资源在合法范围内得以有效使用和管理。权限管理几乎出现在任何系统里面，只要有用户和密码的系统。</p> <h3 id="_2-2-访问控制策略"><a href="#_2-2-访问控制策略" class="header-anchor">#</a> 2.2 访问控制策略</h3> <p>在权限管理中使用最多的还是基于角色访问控制（RBAC：Role Based Access Control）。基于角色的访问控制，是 20 世纪 90 年代研究出来的一种模型。这种模型的基本概念是把许可权（Permission）与角色（Role）联系在一起，用户通过充当合适角色的成员而获得该角色的许可权。</p> <p>例如，在一间公司中，有老板、经理、行政、人事、财务等不同的职位，在通常情况下，职位所赋予的权利是不变的，但在某个职位上工作的人可以根据需要调整。RBAC 模型对组织内部的这些关系与访问控制要求给出了非常恰当的描述。</p> <h3 id="_2-3-rbac-重要对象"><a href="#_2-3-rbac-重要对象" class="header-anchor">#</a> 2.3 RBAC 重要对象</h3> <ul><li>用户（Employee）：角色施加的主体；用户通过拥有某个或多个角色以得到对应的权限。</li> <li>角色（Role）：表示一组权限的集合。</li> <li>权限（Permission）：一个资源代表一个权限，是否能访问该资源，就是看是否有该权限。</li></ul> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20240122153756363.png" alt=""></strong></p> <div class="language- line-numbers-mode"><pre class="language-text"><code>结论:
- 用户和角色是多对多关系
一个用户可以有多个角色
一个角色可以分配给多个用户

- 角色和权限是多对多关系
一个角色有多个权限
一个权限可以分配给多个角色

</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div><h3 id="_2-4-rbac-流程与模型分析"><a href="#_2-4-rbac-流程与模型分析" class="header-anchor">#</a> 2.4 RBAC 流程与模型分析</h3> <p>管理员可为用户分配角色和权限（用户、角色和权限数据还有其之间关系数据都得保存起来）。</p> <blockquote><p>动手画流程图分析以及模型图。</p></blockquote> <h3 id="_2-5-项目演示"><a href="#_2-5-项目演示" class="header-anchor">#</a> 2.5 项目演示</h3> <p>观察演示项目文档, 搞清楚项目要做哪些功能有哪些模块</p> <p><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/1.png" alt=""></p> <h2 id="三、搭建项目"><a href="#三、搭建项目" class="header-anchor">#</a> 三、搭建项目</h2> <h3 id="_3-1-技术选型"><a href="#_3-1-技术选型" class="header-anchor">#</a> 3.1 技术选型</h3> <p>在项目开发前，需要对技术方案先做方向性的评估。在投资有限、硬件资源有限的条件下，为了满足需求，需要进行技术方案选型、技术点使用范围进行分析。也就是需要选择哪种类型脚手架(包含哪些技术的脚手架)</p> <p><strong>常见的技术选型度量点：</strong></p> <p>（1）快速开发</p> <p>（2）学习成本低</p> <p>（3）技术成熟度</p> <p>（4）稳定性</p> <p>（5）性能</p> <p>当前项目技术架构：</p> <p>后端框架：Springboot 、MyBatis、Spring</p> <p>前端框架：Vue 、ElementUi 、Es6 、Axios</p> <p>数据库： MySQL、Redis</p> <p>项目结构:  前后端分离</p> <h3 id="_3-2-导入依赖"><a href="#_3-2-导入依赖" class="header-anchor">#</a> 3.2 导入依赖</h3> <div class="language-xml line-numbers-mode"><pre class="language-xml"><code><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>parent</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">&gt;</span></span>org.springframework.boot<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">&gt;</span></span>spring-boot-starter-parent<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>version</span><span class="token punctuation">&gt;</span></span>2.3.3.RELEASE<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>version</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>relativePath</span><span class="token punctuation">/&gt;</span></span> <span class="token comment">&lt;!-- lookup parent from repository --&gt;</span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>parent</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependencies</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">&gt;</span></span>org.springframework.boot<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">&gt;</span></span>spring-boot-starter<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">&gt;</span></span>org.springframework.boot<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">&gt;</span></span>spring-boot-starter-web<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">&gt;</span></span>org.projectlombok<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">&gt;</span></span>lombok<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>optional</span><span class="token punctuation">&gt;</span></span>true<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>optional</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">&gt;</span></span>org.springframework.boot<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">&gt;</span></span>spring-boot-starter-test<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>scope</span><span class="token punctuation">&gt;</span></span>test<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>scope</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">&gt;</span></span>com.alibaba<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">&gt;</span></span>druid-spring-boot-starter<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>version</span><span class="token punctuation">&gt;</span></span>1.1.21<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>version</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">&gt;</span></span>org.springframework.boot<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">&gt;</span></span>spring-boot-starter-jdbc<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">&gt;</span></span>mysql<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">&gt;</span></span>mysql-connector-java<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>scope</span><span class="token punctuation">&gt;</span></span>runtime<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>scope</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">&gt;</span></span>
    <span class="token comment">&lt;!-- Mybatis 集成到 SpringBoot 中的依赖 --&gt;</span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">&gt;</span></span>org.mybatis.spring.boot<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">&gt;</span></span>mybatis-spring-boot-starter<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>version</span><span class="token punctuation">&gt;</span></span>1.3.1<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>version</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">&gt;</span></span>com.github.pagehelper<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">&gt;</span></span>pagehelper-spring-boot-starter<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>version</span><span class="token punctuation">&gt;</span></span>1.3.0<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>version</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">&gt;</span></span>
    <span class="token comment">&lt;!-- 引入 redis 依赖 --&gt;</span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">&gt;</span></span>org.springframework.boot<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">&gt;</span></span>spring-boot-starter-data-redis<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">&gt;</span></span>org.apache.commons<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">&gt;</span></span>commons-pool2<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">&gt;</span></span>commons-codec<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">&gt;</span></span>commons-codec<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>version</span><span class="token punctuation">&gt;</span></span>1.15<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>version</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>dependency</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">&gt;</span></span>com.alibaba<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">&gt;</span></span>fastjson<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>version</span><span class="token punctuation">&gt;</span></span>1.2.83<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>version</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependency</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>dependencies</span><span class="token punctuation">&gt;</span></span>

<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>build</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>plugins</span><span class="token punctuation">&gt;</span></span>
        <span class="token comment">&lt;!-- Spring Boot 打包插件 --&gt;</span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>plugin</span><span class="token punctuation">&gt;</span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>groupId</span><span class="token punctuation">&gt;</span></span>org.springframework.boot<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>groupId</span><span class="token punctuation">&gt;</span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>artifactId</span><span class="token punctuation">&gt;</span></span>spring-boot-maven-plugin<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>artifactId</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>plugin</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>plugins</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>build</span><span class="token punctuation">&gt;</span></span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br><span class="line-number">39</span><br><span class="line-number">40</span><br><span class="line-number">41</span><br><span class="line-number">42</span><br><span class="line-number">43</span><br><span class="line-number">44</span><br><span class="line-number">45</span><br><span class="line-number">46</span><br><span class="line-number">47</span><br><span class="line-number">48</span><br><span class="line-number">49</span><br><span class="line-number">50</span><br><span class="line-number">51</span><br><span class="line-number">52</span><br><span class="line-number">53</span><br><span class="line-number">54</span><br><span class="line-number">55</span><br><span class="line-number">56</span><br><span class="line-number">57</span><br><span class="line-number">58</span><br><span class="line-number">59</span><br><span class="line-number">60</span><br><span class="line-number">61</span><br><span class="line-number">62</span><br><span class="line-number">63</span><br><span class="line-number">64</span><br><span class="line-number">65</span><br><span class="line-number">66</span><br><span class="line-number">67</span><br><span class="line-number">68</span><br><span class="line-number">69</span><br><span class="line-number">70</span><br><span class="line-number">71</span><br><span class="line-number">72</span><br><span class="line-number">73</span><br><span class="line-number">74</span><br><span class="line-number">75</span><br><span class="line-number">76</span><br><span class="line-number">77</span><br><span class="line-number">78</span><br><span class="line-number">79</span><br><span class="line-number">80</span><br></div></div><h3 id="_3-3-加入配置"><a href="#_3-3-加入配置" class="header-anchor">#</a> 3.3 加入配置</h3> <div class="language-yml line-numbers-mode"><pre class="language-yml"><code><span class="token key atrule">spring</span><span class="token punctuation">:</span>
  <span class="token key atrule">datasource</span><span class="token punctuation">:</span>
    <span class="token key atrule">driver-class-name</span><span class="token punctuation">:</span> com.mysql.jdbc.Driver
    <span class="token key atrule">url</span><span class="token punctuation">:</span> jdbc<span class="token punctuation">:</span>mysql<span class="token punctuation">:</span>//localhost<span class="token punctuation">:</span>3306/rbac<span class="token punctuation">?</span>useUnicode=true<span class="token important">&amp;characterEncoding=utf8&amp;serverTimezone=GMT%2B8</span>
    <span class="token key atrule">username</span><span class="token punctuation">:</span> root
    <span class="token key atrule">password</span><span class="token punctuation">:</span> admin
    <span class="token comment">## redis 配置</span>
  <span class="token key atrule">redis</span><span class="token punctuation">:</span>
    <span class="token comment">## 地址</span>
    <span class="token key atrule">host</span><span class="token punctuation">:</span> 127.0.0.1
    <span class="token comment">## 端口，默认为6379</span>
    <span class="token key atrule">port</span><span class="token punctuation">:</span> <span class="token number">6379</span>
    <span class="token comment">## 数据库索引</span>
    <span class="token key atrule">database</span><span class="token punctuation">:</span> <span class="token number">0</span>
    <span class="token comment">## 密码</span>
    <span class="token key atrule">password</span><span class="token punctuation">:</span>
    <span class="token comment">## 连接超时时间</span>
    <span class="token key atrule">timeout</span><span class="token punctuation">:</span> 10s
    <span class="token key atrule">lettuce</span><span class="token punctuation">:</span>
      <span class="token key atrule">pool</span><span class="token punctuation">:</span>
        <span class="token comment">## 连接池中的最小空闲连接</span>
        <span class="token key atrule">min-idle</span><span class="token punctuation">:</span> <span class="token number">0</span>
        <span class="token comment">## 连接池中的最大空闲连接</span>
        <span class="token key atrule">max-idle</span><span class="token punctuation">:</span> <span class="token number">8</span>
        <span class="token comment">## 连接池的最大数据库连接数</span>
        <span class="token key atrule">max-active</span><span class="token punctuation">:</span> <span class="token number">8</span>
        <span class="token comment">## #连接池最大阻塞等待时间（使用负值表示没有限制）</span>
        <span class="token key atrule">max-wait</span><span class="token punctuation">:</span> <span class="token punctuation">-</span>1ms
  <span class="token key atrule">jackson</span><span class="token punctuation">:</span>
    <span class="token key atrule">default-property-inclusion</span><span class="token punctuation">:</span> non_null
<span class="token key atrule">server</span><span class="token punctuation">:</span>
  <span class="token key atrule">port</span><span class="token punctuation">:</span> <span class="token number">8080</span>
<span class="token key atrule">mybatis</span><span class="token punctuation">:</span>
  <span class="token key atrule">type-aliases-package</span><span class="token punctuation">:</span> cn.wolfcode.rbac.domain
<span class="token key atrule">logging</span><span class="token punctuation">:</span>
  <span class="token key atrule">level</span><span class="token punctuation">:</span>
    <span class="token key atrule">root</span><span class="token punctuation">:</span> info
    <span class="token key atrule">cn</span><span class="token punctuation">:</span>
      <span class="token key atrule">wolfcode</span><span class="token punctuation">:</span>
        <span class="token key atrule">rbac</span><span class="token punctuation">:</span>
          <span class="token key atrule">mapper</span><span class="token punctuation">:</span> trace
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br><span class="line-number">39</span><br><span class="line-number">40</span><br><span class="line-number">41</span><br></div></div><h3 id="_3-4-创建启动类"><a href="#_3-4-创建启动类" class="header-anchor">#</a> 3.4 创建启动类</h3> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token annotation punctuation">@SpringBootApplication</span>
<span class="token annotation punctuation">@MapperScan</span><span class="token punctuation">(</span>basePackages <span class="token operator">=</span> <span class="token string">&quot;cn.wolfcode.rbac.mapper&quot;</span><span class="token punctuation">)</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">App</span> <span class="token punctuation">{</span>
    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token class-name">SpringApplication</span><span class="token punctuation">.</span><span class="token function">run</span><span class="token punctuation">(</span><span class="token class-name">App</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">,</span>args<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><h3 id="_3-5-响应类定义"><a href="#_3-5-响应类定义" class="header-anchor">#</a> 3.5 响应类定义</h3> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">package</span> <span class="token namespace">cn<span class="token punctuation">.</span>wolfcode<span class="token punctuation">.</span>rbac<span class="token punctuation">.</span>util</span><span class="token punctuation">;</span>

<span class="token keyword">import</span> <span class="token import"><span class="token namespace">lombok<span class="token punctuation">.</span></span><span class="token class-name">AllArgsConstructor</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">lombok<span class="token punctuation">.</span></span><span class="token class-name">Data</span></span><span class="token punctuation">;</span>
<span class="token keyword">import</span> <span class="token import"><span class="token namespace">lombok<span class="token punctuation">.</span></span><span class="token class-name">NoArgsConstructor</span></span><span class="token punctuation">;</span>

<span class="token comment">/**
 * Created by wolfcode-lanxw
 */</span>
<span class="token annotation punctuation">@Data</span>
<span class="token annotation punctuation">@AllArgsConstructor</span>
<span class="token annotation punctuation">@NoArgsConstructor</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">R</span> <span class="token punctuation">{</span>
    <span class="token keyword">private</span> <span class="token keyword">int</span> code<span class="token punctuation">;</span><span class="token comment">// 状态码:200成功,500失败</span>
    <span class="token keyword">private</span> <span class="token class-name">String</span> msg<span class="token punctuation">;</span><span class="token comment">// 响应提示的信息</span>
    <span class="token keyword">private</span> <span class="token class-name">Object</span> data<span class="token punctuation">;</span><span class="token comment">// 响应的具体内容</span>
    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token class-name">R</span> <span class="token function">ok</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> <span class="token keyword">new</span> <span class="token class-name">R</span><span class="token punctuation">(</span><span class="token number">200</span><span class="token punctuation">,</span> <span class="token string">&quot;操作成功&quot;</span><span class="token punctuation">,</span> <span class="token keyword">null</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token class-name">R</span> <span class="token function">ok</span><span class="token punctuation">(</span><span class="token class-name">String</span> msg<span class="token punctuation">,</span> <span class="token class-name">Object</span> data<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> <span class="token keyword">new</span> <span class="token class-name">R</span><span class="token punctuation">(</span><span class="token number">200</span><span class="token punctuation">,</span> msg<span class="token punctuation">,</span> data<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token class-name">R</span> <span class="token function">ok</span><span class="token punctuation">(</span><span class="token class-name">Object</span> data<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> <span class="token keyword">new</span> <span class="token class-name">R</span><span class="token punctuation">(</span><span class="token number">200</span><span class="token punctuation">,</span> <span class="token string">&quot;操作成功&quot;</span><span class="token punctuation">,</span> data<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token class-name">R</span> <span class="token function">fail</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> <span class="token keyword">new</span> <span class="token class-name">R</span><span class="token punctuation">(</span><span class="token number">500</span><span class="token punctuation">,</span> <span class="token string">&quot;系统繁忙&quot;</span><span class="token punctuation">,</span> <span class="token keyword">null</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token class-name">R</span> <span class="token function">fail</span><span class="token punctuation">(</span><span class="token class-name">String</span> msg<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> <span class="token keyword">new</span> <span class="token class-name">R</span><span class="token punctuation">(</span><span class="token number">500</span><span class="token punctuation">,</span> msg<span class="token punctuation">,</span><span class="token keyword">null</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token class-name">R</span> <span class="token function">fail</span><span class="token punctuation">(</span><span class="token keyword">int</span> code<span class="token punctuation">,</span><span class="token class-name">String</span> msg<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> <span class="token keyword">new</span> <span class="token class-name">R</span><span class="token punctuation">(</span>code<span class="token punctuation">,</span> msg<span class="token punctuation">,</span><span class="token keyword">null</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br></div></div><h2 id="四、部门管理"><a href="#四、部门管理" class="header-anchor">#</a> 四、部门管理</h2> <p>部门表：department</p> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20240122113201061.png" alt=""></strong></p> <h3 id="_4-1-部门列表"><a href="#_4-1-部门列表" class="header-anchor">#</a> 4.1 部门列表</h3> <p><strong>A) 前端界面</strong></p> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20240122113113723.png" alt=""></strong></p> <p><strong>B) 接口说明</strong></p> <p>部门分页查询</p> <p><strong>C) 请求路径</strong></p> <div class="language- line-numbers-mode"><pre class="language-text"><code>GET /api/departments
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p><strong>D) 请求参数</strong></p> <div class="language- line-numbers-mode"><pre class="language-text"><code>pageNum=1&amp;pageSize=5
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><table><thead><tr><th>名称</th> <th>类型</th> <th>说明</th></tr></thead> <tbody><tr><td>pageNum</td> <td>integer</td> <td>当前页数</td></tr> <tr><td>pageSize</td> <td>integer</td> <td>每页条数</td></tr></tbody></table> <p><strong>E) 响应格式</strong></p> <div class="language-json line-numbers-mode"><pre class="language-json"><code><span class="token punctuation">{</span>
    <span class="token property">&quot;code&quot;</span><span class="token operator">:</span> <span class="token number">200</span><span class="token punctuation">,</span>
    <span class="token property">&quot;msg&quot;</span><span class="token operator">:</span> <span class="token string">&quot;操作成功&quot;</span><span class="token punctuation">,</span>
    <span class="token property">&quot;data&quot;</span><span class="token operator">:</span> <span class="token punctuation">{</span>
        <span class="token property">&quot;pageNum&quot;</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
        <span class="token property">&quot;pageSize&quot;</span><span class="token operator">:</span> <span class="token number">5</span><span class="token punctuation">,</span>
        <span class="token property">&quot;total&quot;</span><span class="token operator">:</span> <span class="token number">6</span><span class="token punctuation">,</span>
        <span class="token property">&quot;list&quot;</span><span class="token operator">:</span> <span class="token punctuation">[</span>
            <span class="token punctuation">{</span>
                <span class="token property">&quot;id&quot;</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
                <span class="token property">&quot;name&quot;</span><span class="token operator">:</span> <span class="token string">&quot;人力资源部&quot;</span><span class="token punctuation">,</span>
                <span class="token property">&quot;sn&quot;</span><span class="token operator">:</span> <span class="token string">&quot;Human Resources Department&quot;</span>
            <span class="token punctuation">}</span><span class="token punctuation">,</span>
            <span class="token punctuation">{</span>
                <span class="token property">&quot;id&quot;</span><span class="token operator">:</span> <span class="token number">2</span><span class="token punctuation">,</span>
                <span class="token property">&quot;name&quot;</span><span class="token operator">:</span> <span class="token string">&quot;采购部&quot;</span><span class="token punctuation">,</span>
                <span class="token property">&quot;sn&quot;</span><span class="token operator">:</span> <span class="token string">&quot;Order Department&quot;</span>
            <span class="token punctuation">}</span><span class="token punctuation">,</span>
            <span class="token punctuation">{</span>
                <span class="token property">&quot;id&quot;</span><span class="token operator">:</span> <span class="token number">3</span><span class="token punctuation">,</span>
                <span class="token property">&quot;name&quot;</span><span class="token operator">:</span> <span class="token string">&quot;仓储部&quot;</span><span class="token punctuation">,</span>
                <span class="token property">&quot;sn&quot;</span><span class="token operator">:</span> <span class="token string">&quot;Warehousing Department&quot;</span>
            <span class="token punctuation">}</span><span class="token punctuation">,</span>
            <span class="token punctuation">{</span>
                <span class="token property">&quot;id&quot;</span><span class="token operator">:</span> <span class="token number">4</span><span class="token punctuation">,</span>
                <span class="token property">&quot;name&quot;</span><span class="token operator">:</span> <span class="token string">&quot;财务部&quot;</span><span class="token punctuation">,</span>
                <span class="token property">&quot;sn&quot;</span><span class="token operator">:</span> <span class="token string">&quot;Finance Department&quot;</span>
            <span class="token punctuation">}</span><span class="token punctuation">,</span>
            <span class="token punctuation">{</span>
                <span class="token property">&quot;id&quot;</span><span class="token operator">:</span> <span class="token number">5</span><span class="token punctuation">,</span>
                <span class="token property">&quot;name&quot;</span><span class="token operator">:</span> <span class="token string">&quot;技术部&quot;</span><span class="token punctuation">,</span>
                <span class="token property">&quot;sn&quot;</span><span class="token operator">:</span> <span class="token string">&quot;Technolog Department &quot;</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">]</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br></div></div><table><thead><tr><th>名称</th> <th>说明</th></tr></thead> <tbody><tr><td>code</td> <td>200成功，401认证失败，403无权限操作，500失败</td></tr> <tr><td>msg</td> <td>操作提示信息</td></tr> <tr><td>data</td> <td>返回参数信息</td></tr></tbody></table> <p><strong>data参数解释:</strong> 存储的分页信息</p> <table><thead><tr><th>名称</th> <th>说明</th></tr></thead> <tbody><tr><td>pageNum</td> <td>当前页码</td></tr> <tr><td>pageSize</td> <td>每页条数</td></tr> <tr><td>total</td> <td>总记录数</td></tr> <tr><td>list</td> <td>数据集合</td></tr></tbody></table> <p><strong>list参数解释:</strong> 存储的是部门对象集合</p> <table><thead><tr><th>名称</th> <th>说明</th></tr></thead> <tbody><tr><td>id</td> <td>部门主键ID</td></tr> <tr><td>name</td> <td>部门名称</td></tr> <tr><td>sn</td> <td>部门编号</td></tr></tbody></table> <p><strong>F) 代码实现</strong></p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token annotation punctuation">@GetMapping</span>
<span class="token keyword">public</span> <span class="token class-name">R</span> <span class="token function">query</span><span class="token punctuation">(</span><span class="token class-name">QueryObject</span> qo<span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token class-name">PageInfo</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Department</span><span class="token punctuation">&gt;</span></span> info <span class="token operator">=</span> departmentService<span class="token punctuation">.</span><span class="token function">selectList</span><span class="token punctuation">(</span>qo<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">return</span> <span class="token class-name">R</span><span class="token punctuation">.</span><span class="token function">ok</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">PageData</span><span class="token punctuation">(</span>info<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><p><strong>G) 测试</strong></p> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20240122102014957.png" alt=""></strong></p> <h3 id="_4-2-部门删除"><a href="#_4-2-部门删除" class="header-anchor">#</a> 4.2 部门删除</h3> <p><strong>A) 前端界面</strong></p> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20240122103644192.png" alt=""></strong></p> <p><strong>B) 接口说明</strong></p> <p>根据ID删除部门</p> <p><strong>C) 请求路径</strong></p> <div class="language- line-numbers-mode"><pre class="language-text"><code>DELETE /api/departments/{id}
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p><strong>D) 请求参数</strong></p> <table><thead><tr><th>名称</th> <th>类型</th> <th>说明</th></tr></thead> <tbody><tr><td>id</td> <td>integer</td> <td>部门ID</td></tr></tbody></table> <p><strong>E) 响应格式</strong></p> <div class="language-json line-numbers-mode"><pre class="language-json"><code><span class="token punctuation">{</span>
    <span class="token property">&quot;code&quot;</span><span class="token operator">:</span> <span class="token number">200</span><span class="token punctuation">,</span>
    <span class="token property">&quot;msg&quot;</span><span class="token operator">:</span> <span class="token string">&quot;操作成功&quot;</span><span class="token punctuation">,</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><table><thead><tr><th>名称</th> <th>说明</th></tr></thead> <tbody><tr><td>code</td> <td>200成功，401认证失败，403无权限操作，500失败</td></tr> <tr><td>msg</td> <td>操作提示信息</td></tr></tbody></table> <p><strong>F) 代码实现</strong></p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token annotation punctuation">@DeleteMapping</span><span class="token punctuation">(</span><span class="token string">&quot;/{id}&quot;</span><span class="token punctuation">)</span>
<span class="token keyword">public</span> <span class="token class-name">R</span> <span class="token function">delete</span><span class="token punctuation">(</span><span class="token annotation punctuation">@PathVariable</span><span class="token punctuation">(</span><span class="token string">&quot;id&quot;</span><span class="token punctuation">)</span> <span class="token class-name">Long</span> id<span class="token punctuation">)</span><span class="token punctuation">{</span>
    departmentService<span class="token punctuation">.</span><span class="token function">delete</span><span class="token punctuation">(</span>id<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">return</span> <span class="token class-name">R</span><span class="token punctuation">.</span><span class="token function">ok</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><p><strong>G) 测试</strong></p> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20240122103028518.png" alt=""></strong></p> <h3 id="_4-3-部门新增"><a href="#_4-3-部门新增" class="header-anchor">#</a> 4.3 部门新增</h3> <p><strong>A) 前端界面</strong></p> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20240122103951348.png" alt=""></strong></p> <p><strong>B) 接口说明</strong></p> <p>新增部门</p> <p><strong>C) 请求路径</strong></p> <div class="language- line-numbers-mode"><pre class="language-text"><code>POST /api/departments
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p><strong>D) 请求参数</strong></p> <div class="language-json line-numbers-mode"><pre class="language-json"><code><span class="token punctuation">{</span>
    <span class="token property">&quot;sn&quot;</span><span class="token operator">:</span> <span class="token string">&quot;Order Department&quot;</span><span class="token punctuation">,</span>
    <span class="token property">&quot;name&quot;</span><span class="token operator">:</span> <span class="token string">&quot;采购部&quot;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><table><thead><tr><th>名称</th> <th>类型</th> <th>说明</th></tr></thead> <tbody><tr><td>sn</td> <td>string</td> <td>部门编号</td></tr> <tr><td>name</td> <td>string</td> <td>部门名称</td></tr></tbody></table> <p><strong>E) 响应格式</strong></p> <div class="language-json line-numbers-mode"><pre class="language-json"><code><span class="token punctuation">{</span>
    <span class="token property">&quot;code&quot;</span><span class="token operator">:</span> <span class="token number">200</span><span class="token punctuation">,</span>
    <span class="token property">&quot;msg&quot;</span><span class="token operator">:</span> <span class="token string">&quot;操作成功&quot;</span><span class="token punctuation">,</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p><strong>F) 代码实现</strong></p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token annotation punctuation">@PostMapping</span>
<span class="token keyword">public</span> <span class="token class-name">R</span> <span class="token function">save</span><span class="token punctuation">(</span><span class="token annotation punctuation">@RequestBody</span> <span class="token class-name">Department</span> department<span class="token punctuation">)</span><span class="token punctuation">{</span>
    departmentService<span class="token punctuation">.</span><span class="token function">save</span><span class="token punctuation">(</span>department<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">return</span> <span class="token class-name">R</span><span class="token punctuation">.</span><span class="token function">ok</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><p><strong>G) 测试</strong></p> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20240122104353541.png" alt=""></strong></p> <h3 id="_4-4-部门更新"><a href="#_4-4-部门更新" class="header-anchor">#</a> 4.4 部门更新</h3> <p><strong>A) 前端界面</strong></p> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20240122104413092.png" alt=""></strong></p> <p><strong>B) 接口说明</strong></p> <p>编辑部门</p> <p><strong>C) 请求路径</strong></p> <div class="language- line-numbers-mode"><pre class="language-text"><code>PUT /api/departments/{id}
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p><strong>D) 请求参数</strong></p> <div class="language-json line-numbers-mode"><pre class="language-json"><code><span class="token punctuation">{</span>
    <span class="token property">&quot;sn&quot;</span><span class="token operator">:</span> <span class="token string">&quot;Order Department&quot;</span><span class="token punctuation">,</span>
    <span class="token property">&quot;name&quot;</span><span class="token operator">:</span> <span class="token string">&quot;资源部&quot;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><table><thead><tr><th>名称</th> <th>类型</th> <th>说明</th></tr></thead> <tbody><tr><td>id</td> <td>integer</td> <td>部门ID</td></tr> <tr><td>sn</td> <td>string</td> <td>部门编号</td></tr> <tr><td>name</td> <td>string</td> <td>部门名称</td></tr></tbody></table> <p><strong>E) 响应格式</strong></p> <div class="language-json line-numbers-mode"><pre class="language-json"><code><span class="token punctuation">{</span>
    <span class="token property">&quot;code&quot;</span><span class="token operator">:</span> <span class="token number">200</span><span class="token punctuation">,</span>
    <span class="token property">&quot;msg&quot;</span><span class="token operator">:</span> <span class="token string">&quot;操作成功&quot;</span><span class="token punctuation">,</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><table><thead><tr><th>名称</th> <th>说明</th></tr></thead> <tbody><tr><td>code</td> <td>200成功，401认证失败，403无权限操作，500失败</td></tr> <tr><td>msg</td> <td>操作提示信息</td></tr></tbody></table> <p><strong>F) 代码实现</strong></p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token annotation punctuation">@PutMapping</span><span class="token punctuation">(</span><span class="token string">&quot;/{id}&quot;</span><span class="token punctuation">)</span>
<span class="token keyword">public</span> <span class="token class-name">R</span> <span class="token function">update</span><span class="token punctuation">(</span><span class="token annotation punctuation">@PathVariable</span><span class="token punctuation">(</span><span class="token string">&quot;id&quot;</span><span class="token punctuation">)</span> <span class="token class-name">Long</span> id<span class="token punctuation">,</span><span class="token annotation punctuation">@RequestBody</span> <span class="token class-name">Department</span> department<span class="token punctuation">)</span><span class="token punctuation">{</span>
    departmentService<span class="token punctuation">.</span><span class="token function">update</span><span class="token punctuation">(</span>id<span class="token punctuation">,</span>department<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">return</span> <span class="token class-name">R</span><span class="token punctuation">.</span><span class="token function">ok</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><p><strong>G) 测试</strong></p> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20240122145443475.png" alt=""></strong></p> <h3 id="_4-5-部门集合"><a href="#_4-5-部门集合" class="header-anchor">#</a> 4.5 部门集合</h3> <p><strong>A) 前端界面</strong></p> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20240122112501569.png" alt=""></strong></p> <p><strong>B) 接口说明</strong></p> <p>用于员工列表页面中部门的高级查询，返回所有部门集合</p> <p><strong>C) 请求路径</strong></p> <div class="language- line-numbers-mode"><pre class="language-text"><code>GET /api/departments
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p><strong>D) 请求头参数</strong></p> <table><thead><tr><th>名称</th> <th>类型</th> <th>说明</th></tr></thead> <tbody><tr><td>cmd</td> <td>string</td> <td>list</td></tr></tbody></table> <p><strong>E) 响应格式</strong></p> <div class="language-json line-numbers-mode"><pre class="language-json"><code><span class="token punctuation">{</span>
    <span class="token property">&quot;code&quot;</span><span class="token operator">:</span> <span class="token number">200</span><span class="token punctuation">,</span>
    <span class="token property">&quot;msg&quot;</span><span class="token operator">:</span> <span class="token string">&quot;操作成功&quot;</span><span class="token punctuation">,</span>
    <span class="token property">&quot;data&quot;</span><span class="token operator">:</span> <span class="token punctuation">[</span>
        <span class="token punctuation">{</span>
            <span class="token property">&quot;id&quot;</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
            <span class="token property">&quot;name&quot;</span><span class="token operator">:</span> <span class="token string">&quot;人力资源部&quot;</span><span class="token punctuation">,</span>
            <span class="token property">&quot;sn&quot;</span><span class="token operator">:</span> <span class="token string">&quot;Human Resources Department&quot;</span>
        <span class="token punctuation">}</span><span class="token punctuation">,</span>
        <span class="token punctuation">{</span>
            <span class="token property">&quot;id&quot;</span><span class="token operator">:</span> <span class="token number">2</span><span class="token punctuation">,</span>
            <span class="token property">&quot;name&quot;</span><span class="token operator">:</span> <span class="token string">&quot;采购部&quot;</span><span class="token punctuation">,</span>
            <span class="token property">&quot;sn&quot;</span><span class="token operator">:</span> <span class="token string">&quot;Order Department&quot;</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">]</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br></div></div><table><thead><tr><th>名称</th> <th>说明</th></tr></thead> <tbody><tr><td>code</td> <td>200成功，401认证失败，403无权限操作，500失败</td></tr> <tr><td>msg</td> <td>操作提示信息</td></tr> <tr><td>data</td> <td>返回的数据</td></tr></tbody></table> <p><strong>data参数解释:</strong> 存储的是部门对象集合</p> <table><thead><tr><th>名称</th> <th>说明</th></tr></thead> <tbody><tr><td>id</td> <td>部门主键ID</td></tr> <tr><td>name</td> <td>部门名称</td></tr> <tr><td>sn</td> <td>部门编号</td></tr></tbody></table> <p><strong>F) 代码实现</strong></p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token annotation punctuation">@GetMapping</span><span class="token punctuation">(</span>headers <span class="token operator">=</span> <span class="token class-name">QueryCommand</span><span class="token punctuation">.</span><span class="token constant">CMD_LIST</span><span class="token punctuation">)</span>
<span class="token keyword">public</span> <span class="token class-name">R</span> <span class="token function">listAll</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token class-name">List</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Department</span><span class="token punctuation">&gt;</span></span> departments <span class="token operator">=</span> departmentService<span class="token punctuation">.</span><span class="token function">selectList</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">return</span> <span class="token class-name">R</span><span class="token punctuation">.</span><span class="token function">ok</span><span class="token punctuation">(</span>departments<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><p><strong>G) 测试</strong></p> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20240122112938256.png" alt=""></strong></p> <h3 id="_4-6-接口统一异常处理"><a href="#_4-6-接口统一异常处理" class="header-anchor">#</a> 4.6 接口统一异常处理</h3> <p>自定义异常:</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">BusinessException</span> <span class="token keyword">extends</span> <span class="token class-name">RuntimeException</span> <span class="token punctuation">{</span>
    <span class="token keyword">public</span> <span class="token class-name">BusinessException</span><span class="token punctuation">(</span><span class="token class-name">String</span> msg<span class="token punctuation">)</span><span class="token punctuation">{</span>
        <span class="token keyword">super</span><span class="token punctuation">(</span>msg<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><p>统一异常处理:</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token annotation punctuation">@ControllerAdvice</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">CommonControllerAdvice</span> <span class="token punctuation">{</span>
    <span class="token annotation punctuation">@ExceptionHandler</span><span class="token punctuation">(</span><span class="token class-name">BusinessException</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span>
    <span class="token annotation punctuation">@ResponseBody</span>
    <span class="token keyword">public</span> <span class="token class-name">R</span> <span class="token function">handleBusinessException</span><span class="token punctuation">(</span><span class="token class-name">BusinessException</span> e<span class="token punctuation">)</span><span class="token punctuation">{</span>
        e<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;BusinessException&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">return</span> <span class="token class-name">R</span><span class="token punctuation">.</span><span class="token function">fail</span><span class="token punctuation">(</span>e<span class="token punctuation">.</span><span class="token function">getMessage</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token annotation punctuation">@ExceptionHandler</span><span class="token punctuation">(</span><span class="token class-name">Exception</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span>
    <span class="token annotation punctuation">@ResponseBody</span>
    <span class="token keyword">public</span> <span class="token class-name">R</span> <span class="token function">handleDefaultException</span><span class="token punctuation">(</span><span class="token class-name">Exception</span> e<span class="token punctuation">)</span><span class="token punctuation">{</span>
        e<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;Exception&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">return</span> <span class="token class-name">R</span><span class="token punctuation">.</span><span class="token function">fail</span><span class="token punctuation">(</span><span class="token string">&quot;系统异常&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br></div></div><h2 id="五、权限管理"><a href="#五、权限管理" class="header-anchor">#</a> 五、权限管理</h2> <p>权限表:</p> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20240122114637265.png" alt=""></strong></p> <h3 id="_5-1-权限列表"><a href="#_5-1-权限列表" class="header-anchor">#</a> 5.1 权限列表</h3> <p><strong>A) 前端界面</strong></p> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20240122114218693.png" alt=""></strong></p> <p><strong>B) 接口说明</strong></p> <p>功能权限分页查询</p> <p><strong>C) 请求路径</strong></p> <div class="language- line-numbers-mode"><pre class="language-text"><code>GET /api/permissions
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p><strong>D) 请求参数</strong></p> <div class="language- line-numbers-mode"><pre class="language-text"><code>pageNum=1&amp;pageSize=5
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><table><thead><tr><th>名称</th> <th>类型</th> <th>说明</th></tr></thead> <tbody><tr><td>pageNum</td> <td>integer</td> <td>当前页数</td></tr> <tr><td>pageSize</td> <td>integer</td> <td>每页条数</td></tr></tbody></table> <p><strong>E) 响应格式</strong></p> <div class="language-json line-numbers-mode"><pre class="language-json"><code><span class="token punctuation">{</span>
    <span class="token property">&quot;code&quot;</span><span class="token operator">:</span> <span class="token number">200</span><span class="token punctuation">,</span>
    <span class="token property">&quot;msg&quot;</span><span class="token operator">:</span> <span class="token string">&quot;操作成功&quot;</span><span class="token punctuation">,</span>
    <span class="token property">&quot;data&quot;</span><span class="token operator">:</span> <span class="token punctuation">{</span>
        <span class="token property">&quot;pageNum&quot;</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
        <span class="token property">&quot;pageSize&quot;</span><span class="token operator">:</span> <span class="token number">5</span><span class="token punctuation">,</span>
        <span class="token property">&quot;total&quot;</span><span class="token operator">:</span> <span class="token number">16</span><span class="token punctuation">,</span>
        <span class="token property">&quot;list&quot;</span><span class="token operator">:</span> <span class="token punctuation">[</span>
            <span class="token punctuation">{</span>
                <span class="token property">&quot;id&quot;</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
                <span class="token property">&quot;name&quot;</span><span class="token operator">:</span> <span class="token string">&quot;ID查询部门&quot;</span><span class="token punctuation">,</span>
                <span class="token property">&quot;expression&quot;</span><span class="token operator">:</span> <span class="token string">&quot;department:get&quot;</span>
            <span class="token punctuation">}</span><span class="token punctuation">,</span>
            <span class="token punctuation">{</span>
                <span class="token property">&quot;id&quot;</span><span class="token operator">:</span> <span class="token number">2</span><span class="token punctuation">,</span>
                <span class="token property">&quot;name&quot;</span><span class="token operator">:</span> <span class="token string">&quot;部门更新&quot;</span><span class="token punctuation">,</span>
                <span class="token property">&quot;expression&quot;</span><span class="token operator">:</span> <span class="token string">&quot;department:update&quot;</span>
            <span class="token punctuation">}</span><span class="token punctuation">,</span>
            <span class="token punctuation">{</span>
                <span class="token property">&quot;id&quot;</span><span class="token operator">:</span> <span class="token number">3</span><span class="token punctuation">,</span>
                <span class="token property">&quot;name&quot;</span><span class="token operator">:</span> <span class="token string">&quot;部门删除&quot;</span><span class="token punctuation">,</span>
                <span class="token property">&quot;expression&quot;</span><span class="token operator">:</span> <span class="token string">&quot;department:delete&quot;</span>
            <span class="token punctuation">}</span><span class="token punctuation">,</span>
            <span class="token punctuation">{</span>
                <span class="token property">&quot;id&quot;</span><span class="token operator">:</span> <span class="token number">4</span><span class="token punctuation">,</span>
                <span class="token property">&quot;name&quot;</span><span class="token operator">:</span> <span class="token string">&quot;部门新增&quot;</span><span class="token punctuation">,</span>
                <span class="token property">&quot;expression&quot;</span><span class="token operator">:</span> <span class="token string">&quot;department:save&quot;</span>
            <span class="token punctuation">}</span><span class="token punctuation">,</span>
            <span class="token punctuation">{</span>
                <span class="token property">&quot;id&quot;</span><span class="token operator">:</span> <span class="token number">5</span><span class="token punctuation">,</span>
                <span class="token property">&quot;name&quot;</span><span class="token operator">:</span> <span class="token string">&quot;部门列表&quot;</span><span class="token punctuation">,</span>
                <span class="token property">&quot;expression&quot;</span><span class="token operator">:</span> <span class="token string">&quot;department:query&quot;</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">]</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br></div></div><table><thead><tr><th>名称</th> <th>说明</th></tr></thead> <tbody><tr><td>code</td> <td>200成功，401认证失败，403无权限操作，500失败</td></tr> <tr><td>msg</td> <td>操作提示信息</td></tr> <tr><td>data</td> <td>返回参数信息</td></tr></tbody></table> <p><strong>data参数解释:</strong> 存储的分页信息</p> <table><thead><tr><th>名称</th> <th>说明</th></tr></thead> <tbody><tr><td>pageNum</td> <td>当前页码</td></tr> <tr><td>pageSize</td> <td>每页条数</td></tr> <tr><td>total</td> <td>数据总数</td></tr> <tr><td>list</td> <td>数据集合</td></tr></tbody></table> <p><strong>list参数解释:</strong> 存储的是部门对象集合</p> <table><thead><tr><th>名称</th> <th>说明</th></tr></thead> <tbody><tr><td>id</td> <td>权限主键ID</td></tr> <tr><td>name</td> <td>权限名称</td></tr> <tr><td>expression</td> <td>权限表达式</td></tr></tbody></table> <p><strong>F) 代码实现</strong></p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token annotation punctuation">@GetMapping</span>
<span class="token keyword">public</span> <span class="token class-name">R</span> <span class="token function">query</span><span class="token punctuation">(</span><span class="token class-name">QueryObject</span> qo<span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token class-name">PageInfo</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Permission</span><span class="token punctuation">&gt;</span></span> info <span class="token operator">=</span> permissionService<span class="token punctuation">.</span><span class="token function">selectList</span><span class="token punctuation">(</span>qo<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">return</span> <span class="token class-name">R</span><span class="token punctuation">.</span><span class="token function">ok</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">PageData</span><span class="token punctuation">(</span>info<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><p><strong>G) 测试</strong></p> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20240122115055426.png" alt=""></strong></p> <h3 id="_5-2-加载权限"><a href="#_5-2-加载权限" class="header-anchor">#</a> 5.2 加载权限</h3> <p><strong>A) 前端界面</strong></p> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20240122115115109.png" alt=""></strong></p> <p><strong>B) 接口说明</strong></p> <p>加载后台所有的@RequiredPermission注解，并将信息存储到数据库中</p> <p><strong>C) 请求路径</strong></p> <div class="language- line-numbers-mode"><pre class="language-text"><code>POST /api/permissions/load
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p><strong>D) 请求参数</strong></p> <p>无</p> <p><strong>E) 响应格式</strong></p> <div class="language-json line-numbers-mode"><pre class="language-json"><code><span class="token punctuation">{</span>
    <span class="token property">&quot;code&quot;</span><span class="token operator">:</span> <span class="token number">200</span><span class="token punctuation">,</span>
    <span class="token property">&quot;msg&quot;</span><span class="token operator">:</span> <span class="token string">&quot;操作成功&quot;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><table><thead><tr><th>名称</th> <th>说明</th></tr></thead> <tbody><tr><td>code</td> <td>200成功，401认证失败，403无权限操作，500失败</td></tr> <tr><td>msg</td> <td>操作提示信息</td></tr></tbody></table> <p><strong>F) 代码实现</strong></p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token annotation punctuation">@PostMapping</span><span class="token punctuation">(</span><span class="token string">&quot;/load&quot;</span><span class="token punctuation">)</span>
<span class="token keyword">public</span> <span class="token class-name">R</span> <span class="token function">load</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    permissionService<span class="token punctuation">.</span><span class="token function">reload</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">return</span> <span class="token class-name">R</span><span class="token punctuation">.</span><span class="token function">ok</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><p><strong>G) 测试</strong></p> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20240122115411401.png" alt=""></strong></p> <h3 id="_5-3-权限集合"><a href="#_5-3-权限集合" class="header-anchor">#</a> 5.3 权限集合</h3> <p><strong>A) 前端界面</strong></p> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20240122115452806.png" alt=""></strong></p> <p><strong>B) 接口说明</strong></p> <p>用于角色新增页面/编辑页面，显示所有的权限集合</p> <p><strong>C) 请求路径</strong></p> <div class="language- line-numbers-mode"><pre class="language-text"><code>GET /api/permissions
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p><strong>D) 请求头参数</strong></p> <table><thead><tr><th>名称</th> <th>类型</th> <th>说明</th></tr></thead> <tbody><tr><td>cmd</td> <td>string</td> <td>list</td></tr></tbody></table> <p><strong>E) 响应格式</strong></p> <div class="language-json line-numbers-mode"><pre class="language-json"><code><span class="token punctuation">{</span>
    <span class="token property">&quot;code&quot;</span><span class="token operator">:</span> <span class="token number">200</span><span class="token punctuation">,</span>
    <span class="token property">&quot;msg&quot;</span><span class="token operator">:</span> <span class="token string">&quot;操作成功&quot;</span><span class="token punctuation">,</span>
    <span class="token property">&quot;data&quot;</span><span class="token operator">:</span> <span class="token punctuation">[</span>
        <span class="token punctuation">{</span>
            <span class="token property">&quot;id&quot;</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
            <span class="token property">&quot;name&quot;</span><span class="token operator">:</span> <span class="token string">&quot;ID查询部门&quot;</span><span class="token punctuation">,</span>
            <span class="token property">&quot;expression&quot;</span><span class="token operator">:</span> <span class="token string">&quot;department:get&quot;</span>
        <span class="token punctuation">}</span><span class="token punctuation">,</span>
        <span class="token punctuation">{</span>
            <span class="token property">&quot;id&quot;</span><span class="token operator">:</span> <span class="token number">2</span><span class="token punctuation">,</span>
            <span class="token property">&quot;name&quot;</span><span class="token operator">:</span> <span class="token string">&quot;部门更新&quot;</span><span class="token punctuation">,</span>
            <span class="token property">&quot;expression&quot;</span><span class="token operator">:</span> <span class="token string">&quot;department:update&quot;</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">]</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br></div></div><table><thead><tr><th>名称</th> <th>说明</th></tr></thead> <tbody><tr><td>code</td> <td>200成功，401认证失败，403无权限操作，500失败</td></tr> <tr><td>msg</td> <td>操作提示信息</td></tr> <tr><td>data</td> <td>返回的数据</td></tr></tbody></table> <p><strong>data参数解释:</strong> 存储的是部门对象集合</p> <table><thead><tr><th>名称</th> <th>说明</th></tr></thead> <tbody><tr><td>id</td> <td>权限主键ID</td></tr> <tr><td>name</td> <td>权限名称</td></tr> <tr><td>expression</td> <td>权限表达式</td></tr></tbody></table> <p><strong>F) 代码实现</strong></p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token annotation punctuation">@GetMapping</span><span class="token punctuation">(</span>headers <span class="token operator">=</span> <span class="token class-name">QueryCommand</span><span class="token punctuation">.</span><span class="token constant">CMD_LIST</span><span class="token punctuation">)</span>
<span class="token keyword">public</span> <span class="token class-name">R</span> <span class="token function">list</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token class-name">List</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Permission</span><span class="token punctuation">&gt;</span></span> permissions <span class="token operator">=</span> permissionService<span class="token punctuation">.</span><span class="token function">listAll</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">return</span> <span class="token class-name">R</span><span class="token punctuation">.</span><span class="token function">ok</span><span class="token punctuation">(</span>permissions<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><p><strong>G) 测试</strong></p> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20240122115650687.png" alt=""></strong></p> <h2 id="六、角色管理"><a href="#六、角色管理" class="header-anchor">#</a> 六、角色管理</h2> <h3 id="_6-1-角色列表"><a href="#_6-1-角色列表" class="header-anchor">#</a> 6.1 角色列表</h3> <p><strong>A) 前端界面</strong></p> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20240122115806508.png" alt=""></strong></p> <p><strong>B) 接口说明</strong></p> <p>角色分页查询</p> <p><strong>C) 请求路径</strong></p> <div class="language- line-numbers-mode"><pre class="language-text"><code>GET /api/roles
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p><strong>D) 请求参数</strong></p> <div class="language- line-numbers-mode"><pre class="language-text"><code>pageNum=1&amp;pageSize=5
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><table><thead><tr><th>名称</th> <th>类型</th> <th>说明</th></tr></thead> <tbody><tr><td>pageNum</td> <td>integer</td> <td>当前页数</td></tr> <tr><td>pageSize</td> <td>integer</td> <td>每页条数</td></tr></tbody></table> <p><strong>E) 响应格式</strong></p> <div class="language-json line-numbers-mode"><pre class="language-json"><code><span class="token punctuation">{</span>
    <span class="token property">&quot;code&quot;</span><span class="token operator">:</span> <span class="token number">200</span><span class="token punctuation">,</span>
    <span class="token property">&quot;msg&quot;</span><span class="token operator">:</span> <span class="token string">&quot;操作成功&quot;</span><span class="token punctuation">,</span>
    <span class="token property">&quot;data&quot;</span><span class="token operator">:</span> <span class="token punctuation">{</span>
        <span class="token property">&quot;pageNum&quot;</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
        <span class="token property">&quot;pageSize&quot;</span><span class="token operator">:</span> <span class="token number">5</span><span class="token punctuation">,</span>
        <span class="token property">&quot;total&quot;</span><span class="token operator">:</span> <span class="token number">10</span><span class="token punctuation">,</span>
        <span class="token property">&quot;list&quot;</span><span class="token operator">:</span> <span class="token punctuation">[</span>
            <span class="token punctuation">{</span>
                <span class="token property">&quot;id&quot;</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
                <span class="token property">&quot;name&quot;</span><span class="token operator">:</span> <span class="token string">&quot;人事管理&quot;</span><span class="token punctuation">,</span>
                <span class="token property">&quot;sn&quot;</span><span class="token operator">:</span> <span class="token string">&quot;HR_MGR&quot;</span>
            <span class="token punctuation">}</span><span class="token punctuation">,</span>
            <span class="token punctuation">{</span>
                <span class="token property">&quot;id&quot;</span><span class="token operator">:</span> <span class="token number">2</span><span class="token punctuation">,</span>
                <span class="token property">&quot;name&quot;</span><span class="token operator">:</span> <span class="token string">&quot;采购管理&quot;</span><span class="token punctuation">,</span>
                <span class="token property">&quot;sn&quot;</span><span class="token operator">:</span> <span class="token string">&quot;ORDER_MGR&quot;</span>
            <span class="token punctuation">}</span><span class="token punctuation">,</span>
            <span class="token punctuation">{</span>
                <span class="token property">&quot;id&quot;</span><span class="token operator">:</span> <span class="token number">3</span><span class="token punctuation">,</span>
                <span class="token property">&quot;name&quot;</span><span class="token operator">:</span> <span class="token string">&quot;仓储管理&quot;</span><span class="token punctuation">,</span>
                <span class="token property">&quot;sn&quot;</span><span class="token operator">:</span> <span class="token string">&quot;WAREHOUSING_MGR&quot;</span>
            <span class="token punctuation">}</span><span class="token punctuation">,</span>
            <span class="token punctuation">{</span>
                <span class="token property">&quot;id&quot;</span><span class="token operator">:</span> <span class="token number">4</span><span class="token punctuation">,</span>
                <span class="token property">&quot;name&quot;</span><span class="token operator">:</span> <span class="token string">&quot;行政部管理&quot;</span><span class="token punctuation">,</span>
                <span class="token property">&quot;sn&quot;</span><span class="token operator">:</span> <span class="token string">&quot;Admin_MGR&quot;</span>
            <span class="token punctuation">}</span><span class="token punctuation">,</span>
            <span class="token punctuation">{</span>
                <span class="token property">&quot;id&quot;</span><span class="token operator">:</span> <span class="token number">11</span><span class="token punctuation">,</span>
                <span class="token property">&quot;name&quot;</span><span class="token operator">:</span> <span class="token string">&quot;市场经理&quot;</span><span class="token punctuation">,</span>
                <span class="token property">&quot;sn&quot;</span><span class="token operator">:</span> <span class="token string">&quot;Market_Manager&quot;</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">]</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br></div></div><table><thead><tr><th>名称</th> <th>说明</th></tr></thead> <tbody><tr><td>code</td> <td>200成功，401认证失败，403无权限操作，500失败</td></tr> <tr><td>msg</td> <td>操作提示信息</td></tr> <tr><td>data</td> <td>返回参数信息</td></tr></tbody></table> <p><strong>data参数解释:</strong> 存储的分页信息</p> <table><thead><tr><th>名称</th> <th>说明</th></tr></thead> <tbody><tr><td>pageNum</td> <td>当前页码</td></tr> <tr><td>pageSize</td> <td>每页条数</td></tr> <tr><td>total</td> <td>数据总数</td></tr> <tr><td>list</td> <td>数据集合</td></tr></tbody></table> <p><strong>list参数解释:</strong> 存储的是部门对象集合</p> <table><thead><tr><th>名称</th> <th>说明</th></tr></thead> <tbody><tr><td>id</td> <td>角色主键ID</td></tr> <tr><td>name</td> <td>角色名称</td></tr> <tr><td>sn</td> <td>角色编号</td></tr></tbody></table> <p><em>F) 代码实现</em>*</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token annotation punctuation">@GetMapping</span>
<span class="token keyword">public</span> <span class="token class-name">R</span> <span class="token function">query</span><span class="token punctuation">(</span><span class="token class-name">QueryObject</span> qo<span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token class-name">PageInfo</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Role</span><span class="token punctuation">&gt;</span></span> info <span class="token operator">=</span> roleService<span class="token punctuation">.</span><span class="token function">selectList</span><span class="token punctuation">(</span>qo<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">return</span> <span class="token class-name">R</span><span class="token punctuation">.</span><span class="token function">ok</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">PageData</span><span class="token punctuation">(</span>info<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><p><strong>G) 测试</strong></p> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20240122120122150.png" alt=""></strong></p> <h3 id="_6-2-角色新增"><a href="#_6-2-角色新增" class="header-anchor">#</a> 6.2 角色新增</h3> <p><strong>A) 前端界面</strong></p> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20240122120151505.png" alt=""></strong></p> <p><strong>B) 接口说明</strong></p> <p>角色新增功能</p> <p><strong>C) 请求路径</strong></p> <div class="language- line-numbers-mode"><pre class="language-text"><code>POST /api/roles
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p><strong>D) 请求参数</strong></p> <div class="language-json line-numbers-mode"><pre class="language-json"><code><span class="token punctuation">{</span>
    <span class="token property">&quot;role&quot;</span><span class="token operator">:</span><span class="token punctuation">{</span>
        <span class="token property">&quot;sn&quot;</span><span class="token operator">:</span><span class="token string">&quot;DEV&quot;</span><span class="token punctuation">,</span>
        <span class="token property">&quot;name&quot;</span><span class="token operator">:</span><span class="token string">&quot;普通开发&quot;</span>
    <span class="token punctuation">}</span><span class="token punctuation">,</span>
    <span class="token property">&quot;permissionIds&quot;</span><span class="token operator">:</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token number">4</span><span class="token punctuation">,</span><span class="token number">5</span><span class="token punctuation">]</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><table><thead><tr><th>名称</th> <th>类型</th> <th>说明</th></tr></thead> <tbody><tr><td>role.sn</td> <td>string</td> <td>角色编号</td></tr> <tr><td>role.name</td> <td>string</td> <td>角色名称</td></tr> <tr><td>permissionIds</td> <td>array</td> <td>权限ID集合</td></tr></tbody></table> <p><strong>E) 响应格式</strong></p> <div class="language-json line-numbers-mode"><pre class="language-json"><code><span class="token punctuation">{</span>
    <span class="token property">&quot;code&quot;</span><span class="token operator">:</span> <span class="token number">200</span><span class="token punctuation">,</span>
    <span class="token property">&quot;msg&quot;</span><span class="token operator">:</span> <span class="token string">&quot;操作成功&quot;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><table><thead><tr><th>名称</th> <th>说明</th></tr></thead> <tbody><tr><td>code</td> <td>200成功，401认证失败，403无权限操作，500失败</td></tr> <tr><td>msg</td> <td>操作提示信息</td></tr></tbody></table> <p><strong>F) 代码实现</strong></p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token annotation punctuation">@PostMapping</span>
<span class="token keyword">public</span> <span class="token class-name">R</span> <span class="token function">save</span><span class="token punctuation">(</span><span class="token annotation punctuation">@RequestBody</span> <span class="token class-name">RoleVo</span> roleVo<span class="token punctuation">)</span><span class="token punctuation">{</span>
    roleService<span class="token punctuation">.</span><span class="token function">save</span><span class="token punctuation">(</span>roleVo<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">return</span> <span class="token class-name">R</span><span class="token punctuation">.</span><span class="token function">ok</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><p><strong>G) 测试</strong></p> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20240122120810377.png" alt=""></strong></p> <h3 id="_6-3-角色更新"><a href="#_6-3-角色更新" class="header-anchor">#</a> 6.3 角色更新</h3> <p><strong>A) 前端界面</strong></p> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20240122120855292.png" alt=""></strong></p> <p><strong>B) 接口说明</strong></p> <p>角色更新</p> <p><strong>C) 请求路径</strong></p> <div class="language- line-numbers-mode"><pre class="language-text"><code>PUT /api/roles/{id}
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p><strong>D) 请求参数</strong></p> <div class="language-json line-numbers-mode"><pre class="language-json"><code><span class="token punctuation">{</span>
    <span class="token property">&quot;role&quot;</span><span class="token operator">:</span><span class="token punctuation">{</span>
        <span class="token property">&quot;sn&quot;</span><span class="token operator">:</span><span class="token string">&quot;DEV&quot;</span><span class="token punctuation">,</span>
        <span class="token property">&quot;name&quot;</span><span class="token operator">:</span><span class="token string">&quot;普通开发&quot;</span>
    <span class="token punctuation">}</span><span class="token punctuation">,</span>
    <span class="token property">&quot;permissionIds&quot;</span><span class="token operator">:</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token number">4</span><span class="token punctuation">,</span><span class="token number">5</span><span class="token punctuation">]</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><table><thead><tr><th>名称</th> <th>类型</th> <th>说明</th></tr></thead> <tbody><tr><td>id</td> <td>long</td> <td>角色ID</td></tr> <tr><td>role.sn</td> <td>string</td> <td>角色编号</td></tr> <tr><td>role.name</td> <td>string</td> <td>角色名称</td></tr> <tr><td>permissionIds</td> <td>array</td> <td>权限ID集合</td></tr></tbody></table> <p><strong>E) 响应格式</strong></p> <div class="language-json line-numbers-mode"><pre class="language-json"><code><span class="token punctuation">{</span>
    <span class="token property">&quot;code&quot;</span><span class="token operator">:</span> <span class="token number">200</span><span class="token punctuation">,</span>
    <span class="token property">&quot;msg&quot;</span><span class="token operator">:</span> <span class="token string">&quot;操作成功&quot;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><table><thead><tr><th>名称</th> <th>说明</th></tr></thead> <tbody><tr><td>code</td> <td>200成功，401认证失败，403无权限操作，500失败</td></tr> <tr><td>msg</td> <td>操作提示信息</td></tr></tbody></table> <p><strong>F) 代码实现</strong></p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token annotation punctuation">@PutMapping</span><span class="token punctuation">(</span><span class="token string">&quot;{id}&quot;</span><span class="token punctuation">)</span>
<span class="token keyword">public</span> <span class="token class-name">R</span> <span class="token function">update</span><span class="token punctuation">(</span><span class="token annotation punctuation">@PathVariable</span><span class="token punctuation">(</span><span class="token string">&quot;id&quot;</span><span class="token punctuation">)</span> <span class="token class-name">Long</span> id<span class="token punctuation">,</span><span class="token annotation punctuation">@RequestBody</span> <span class="token class-name">RoleVo</span> roleVo<span class="token punctuation">)</span><span class="token punctuation">{</span>
    roleService<span class="token punctuation">.</span><span class="token function">update</span><span class="token punctuation">(</span>id<span class="token punctuation">,</span>roleVo<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">return</span> <span class="token class-name">R</span><span class="token punctuation">.</span><span class="token function">ok</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token annotation punctuation">@Override</span>
<span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">update</span><span class="token punctuation">(</span><span class="token class-name">Long</span> id<span class="token punctuation">,</span><span class="token class-name">RoleVo</span> roleVo<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token class-name">Role</span> role <span class="token operator">=</span> roleVo<span class="token punctuation">.</span><span class="token function">getRole</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">if</span><span class="token punctuation">(</span>id<span class="token operator">==</span><span class="token keyword">null</span> <span class="token operator">||</span> role<span class="token operator">==</span><span class="token keyword">null</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
        <span class="token keyword">throw</span> <span class="token keyword">new</span> <span class="token class-name">BusinessException</span><span class="token punctuation">(</span><span class="token string">&quot;非法操作&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token class-name">Long</span><span class="token punctuation">[</span><span class="token punctuation">]</span> permissionIds <span class="token operator">=</span> roleVo<span class="token punctuation">.</span><span class="token function">getPermissionIds</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    role<span class="token punctuation">.</span><span class="token function">setId</span><span class="token punctuation">(</span>id<span class="token punctuation">)</span><span class="token punctuation">;</span>
    roleMapper<span class="token punctuation">.</span><span class="token function">updateByPrimaryKey</span><span class="token punctuation">(</span>role<span class="token punctuation">)</span><span class="token punctuation">;</span>
    roleMapper<span class="token punctuation">.</span><span class="token function">deletePermissionRelationByRoleId</span><span class="token punctuation">(</span>role<span class="token punctuation">.</span><span class="token function">getId</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">if</span><span class="token punctuation">(</span>permissionIds<span class="token operator">!=</span><span class="token keyword">null</span> <span class="token operator">&amp;&amp;</span> permissionIds<span class="token punctuation">.</span>length<span class="token operator">&gt;</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
        roleMapper<span class="token punctuation">.</span><span class="token function">insertPermissionRelationBatch</span><span class="token punctuation">(</span>role<span class="token punctuation">.</span><span class="token function">getId</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span>permissionIds<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br></div></div><p><strong>G) 测试</strong></p> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20240122145823955.png" alt=""></strong></p> <h3 id="_6-4-角色删除"><a href="#_6-4-角色删除" class="header-anchor">#</a> 6.4 角色删除</h3> <p><strong>A) 前端界面</strong></p> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20240122121259778.png" alt=""></strong></p> <p><strong>B) 接口说明</strong></p> <p>删除角色，需要将权限的关系删除掉</p> <p><strong>C) 请求路径</strong></p> <div class="language- line-numbers-mode"><pre class="language-text"><code>DELETE /api/roles/{id}
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p><strong>D) 请求参数</strong></p> <table><thead><tr><th>名称</th> <th>类型</th> <th>说明</th></tr></thead> <tbody><tr><td>id</td> <td>int</td> <td>角色ID</td></tr></tbody></table> <p><strong>E) 响应格式</strong></p> <div class="language-json line-numbers-mode"><pre class="language-json"><code><span class="token punctuation">{</span>
    <span class="token property">&quot;code&quot;</span><span class="token operator">:</span> <span class="token number">200</span><span class="token punctuation">,</span>
    <span class="token property">&quot;msg&quot;</span><span class="token operator">:</span> <span class="token string">&quot;操作成功&quot;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><table><thead><tr><th>名称</th> <th>说明</th></tr></thead> <tbody><tr><td>code</td> <td>200成功，401认证失败，403无权限操作，500失败</td></tr> <tr><td>msg</td> <td>操作提示信息</td></tr></tbody></table> <p><strong>F) 代码实现</strong></p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token annotation punctuation">@DeleteMapping</span><span class="token punctuation">(</span><span class="token string">&quot;/{id}&quot;</span><span class="token punctuation">)</span>
<span class="token keyword">public</span> <span class="token class-name">R</span> <span class="token function">delete</span><span class="token punctuation">(</span><span class="token annotation punctuation">@PathVariable</span><span class="token punctuation">(</span><span class="token string">&quot;id&quot;</span><span class="token punctuation">)</span> <span class="token class-name">Long</span> id<span class="token punctuation">)</span><span class="token punctuation">{</span>
    roleService<span class="token punctuation">.</span><span class="token function">delete</span><span class="token punctuation">(</span>id<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">return</span> <span class="token class-name">R</span><span class="token punctuation">.</span><span class="token function">ok</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token annotation punctuation">@Override</span>
<span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">delete</span><span class="token punctuation">(</span><span class="token class-name">Long</span> id<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">if</span><span class="token punctuation">(</span>id<span class="token operator">==</span><span class="token keyword">null</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
        <span class="token keyword">throw</span> <span class="token keyword">new</span> <span class="token class-name">BusinessException</span><span class="token punctuation">(</span><span class="token string">&quot;非法操作&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">int</span> count <span class="token operator">=</span> roleMapper<span class="token punctuation">.</span><span class="token function">deleteByPrimaryKey</span><span class="token punctuation">(</span>id<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">if</span><span class="token punctuation">(</span>count<span class="token operator">&gt;</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
        <span class="token comment">//删除中间表的关系</span>
        roleMapper<span class="token punctuation">.</span><span class="token function">deletePermissionRelationByRoleId</span><span class="token punctuation">(</span>id<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br></div></div><p><strong>G) 测试</strong></p> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20240122121543707.png" alt=""></strong></p> <h3 id="_6-5-角色集合"><a href="#_6-5-角色集合" class="header-anchor">#</a> 6.5 角色集合</h3> <p><strong>A) 前端界面</strong></p> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20240122121718621.png" alt=""></strong></p> <p><strong>B) 接口说明</strong></p> <p>用户员工新增/员工编辑页面中展示所有的角色</p> <p><strong>C) 请求路径</strong></p> <div class="language- line-numbers-mode"><pre class="language-text"><code>GET /api/roles
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p><strong>D) 请求头参数</strong></p> <table><thead><tr><th>名称</th> <th>类型</th> <th>说明</th></tr></thead> <tbody><tr><td>cmd</td> <td>string</td> <td>list</td></tr></tbody></table> <p><strong>E) 响应格式</strong></p> <div class="language-json line-numbers-mode"><pre class="language-json"><code><span class="token punctuation">{</span>
    <span class="token property">&quot;code&quot;</span><span class="token operator">:</span> <span class="token number">200</span><span class="token punctuation">,</span>
    <span class="token property">&quot;msg&quot;</span><span class="token operator">:</span> <span class="token string">&quot;操作成功&quot;</span><span class="token punctuation">,</span>
    <span class="token property">&quot;data&quot;</span><span class="token operator">:</span> <span class="token punctuation">[</span>
        <span class="token punctuation">{</span>
            <span class="token property">&quot;id&quot;</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
            <span class="token property">&quot;name&quot;</span><span class="token operator">:</span> <span class="token string">&quot;人事管理&quot;</span><span class="token punctuation">,</span>
            <span class="token property">&quot;sn&quot;</span><span class="token operator">:</span> <span class="token string">&quot;HR_MGR&quot;</span>
        <span class="token punctuation">}</span><span class="token punctuation">,</span>
        <span class="token punctuation">{</span>
            <span class="token property">&quot;id&quot;</span><span class="token operator">:</span> <span class="token number">2</span><span class="token punctuation">,</span>
            <span class="token property">&quot;name&quot;</span><span class="token operator">:</span> <span class="token string">&quot;采购管理&quot;</span><span class="token punctuation">,</span>
            <span class="token property">&quot;sn&quot;</span><span class="token operator">:</span> <span class="token string">&quot;ORDER_MGR&quot;</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">]</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br></div></div><table><thead><tr><th>名称</th> <th>说明</th></tr></thead> <tbody><tr><td>code</td> <td>200成功，401认证失败，403无权限操作，500失败</td></tr> <tr><td>msg</td> <td>操作提示信息</td></tr> <tr><td>data</td> <td>返回的数据</td></tr></tbody></table> <p><strong>data参数解释:</strong> 存储的是部门对象集合</p> <table><thead><tr><th>名称</th> <th>说明</th></tr></thead> <tbody><tr><td>id</td> <td>角色主键ID</td></tr> <tr><td>name</td> <td>角色名称</td></tr> <tr><td>sn</td> <td>角色编号</td></tr></tbody></table> <p><strong>F) 代码实现</strong></p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token annotation punctuation">@GetMapping</span><span class="token punctuation">(</span>headers <span class="token operator">=</span> <span class="token class-name">QueryCommand</span><span class="token punctuation">.</span><span class="token constant">CMD_LIST</span><span class="token punctuation">)</span>
<span class="token keyword">public</span> <span class="token class-name">R</span> <span class="token function">listAll</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token class-name">List</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Role</span><span class="token punctuation">&gt;</span></span> roles <span class="token operator">=</span> roleService<span class="token punctuation">.</span><span class="token function">listAll</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">return</span> <span class="token class-name">R</span><span class="token punctuation">.</span><span class="token function">ok</span><span class="token punctuation">(</span>roles<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><p><strong>G) 测试</strong></p> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20240122122026480.png" alt=""></strong></p> <h2 id="七、员工管理"><a href="#七、员工管理" class="header-anchor">#</a> 七、员工管理</h2> <h3 id="_7-1-员工列表"><a href="#_7-1-员工列表" class="header-anchor">#</a> 7.1 员工列表</h3> <p><strong>A) 前端界面</strong></p> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20240122122221753.png" alt=""></strong></p> <p><strong>B) 接口说明</strong></p> <p>分页查询员工列表，需要完成高级查询功能</p> <p><strong>C) 请求路径</strong></p> <div class="language- line-numbers-mode"><pre class="language-text"><code>GET /api/employees
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p><strong>D) 请求参数</strong></p> <div class="language- line-numbers-mode"><pre class="language-text"><code>pageNum=1&amp;pageSize=5&amp;key=明&amp;deptId=-1
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><table><thead><tr><th>名称</th> <th>类型</th> <th>说明</th></tr></thead> <tbody><tr><td>pageNum</td> <td>integer</td> <td>当前页数</td></tr> <tr><td>pageSize</td> <td>integer</td> <td>每页条数</td></tr> <tr><td>key</td> <td>string</td> <td>搜索的文本</td></tr> <tr><td>deptId</td> <td>long</td> <td>部门ID</td></tr></tbody></table> <p><strong>E) 响应格式</strong></p> <div class="language-json line-numbers-mode"><pre class="language-json"><code><span class="token punctuation">{</span>
    <span class="token property">&quot;code&quot;</span><span class="token operator">:</span> <span class="token number">200</span><span class="token punctuation">,</span>
    <span class="token property">&quot;msg&quot;</span><span class="token operator">:</span> <span class="token string">&quot;操作成功&quot;</span><span class="token punctuation">,</span>
    <span class="token property">&quot;data&quot;</span><span class="token operator">:</span> <span class="token punctuation">{</span>
        <span class="token property">&quot;pageNum&quot;</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
        <span class="token property">&quot;pageSize&quot;</span><span class="token operator">:</span> <span class="token number">5</span><span class="token punctuation">,</span>
        <span class="token property">&quot;total&quot;</span><span class="token operator">:</span> <span class="token number">20</span><span class="token punctuation">,</span>
        <span class="token property">&quot;list&quot;</span><span class="token operator">:</span> <span class="token punctuation">[</span>
            <span class="token punctuation">{</span>
                <span class="token property">&quot;id&quot;</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
                <span class="token property">&quot;name&quot;</span><span class="token operator">:</span> <span class="token string">&quot;admin&quot;</span><span class="token punctuation">,</span>
                <span class="token property">&quot;password&quot;</span><span class="token operator">:</span> <span class="token string">&quot;123&quot;</span><span class="token punctuation">,</span>
                <span class="token property">&quot;email&quot;</span><span class="token operator">:</span> <span class="token string">&quot;admin@qq.com&quot;</span><span class="token punctuation">,</span>
                <span class="token property">&quot;age&quot;</span><span class="token operator">:</span> <span class="token number">18</span><span class="token punctuation">,</span>
                <span class="token property">&quot;admin&quot;</span><span class="token operator">:</span> <span class="token boolean">true</span><span class="token punctuation">,</span>
                <span class="token property">&quot;department&quot;</span><span class="token operator">:</span> <span class="token punctuation">{</span>
                    <span class="token property">&quot;id&quot;</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
                    <span class="token property">&quot;name&quot;</span><span class="token operator">:</span> <span class="token string">&quot;人力资源部&quot;</span><span class="token punctuation">,</span>
                    <span class="token property">&quot;sn&quot;</span><span class="token operator">:</span> <span class="token string">&quot;Human Resources Department&quot;</span>
                <span class="token punctuation">}</span>
            <span class="token punctuation">}</span><span class="token punctuation">,</span>
            <span class="token punctuation">{</span>
                <span class="token property">&quot;id&quot;</span><span class="token operator">:</span> <span class="token number">2</span><span class="token punctuation">,</span>
                <span class="token property">&quot;name&quot;</span><span class="token operator">:</span> <span class="token string">&quot;赵总&quot;</span><span class="token punctuation">,</span>
                <span class="token property">&quot;password&quot;</span><span class="token operator">:</span> <span class="token string">&quot;123&quot;</span><span class="token punctuation">,</span>
                <span class="token property">&quot;email&quot;</span><span class="token operator">:</span> <span class="token string">&quot;zhaoz@wolfcode.cn&quot;</span><span class="token punctuation">,</span>
                <span class="token property">&quot;age&quot;</span><span class="token operator">:</span> <span class="token number">35</span><span class="token punctuation">,</span>
                <span class="token property">&quot;admin&quot;</span><span class="token operator">:</span> <span class="token boolean">true</span><span class="token punctuation">,</span>
                <span class="token property">&quot;department&quot;</span><span class="token operator">:</span> <span class="token punctuation">{</span>
                    <span class="token property">&quot;id&quot;</span><span class="token operator">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
                    <span class="token property">&quot;name&quot;</span><span class="token operator">:</span> <span class="token string">&quot;人力资源部&quot;</span><span class="token punctuation">,</span>
                    <span class="token property">&quot;sn&quot;</span><span class="token operator">:</span> <span class="token string">&quot;Human Resources Department&quot;</span>
                <span class="token punctuation">}</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">]</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br></div></div><table><thead><tr><th>名称</th> <th>说明</th></tr></thead> <tbody><tr><td>code</td> <td>200成功，401认证失败，403无权限操作，500失败</td></tr> <tr><td>msg</td> <td>操作提示信息</td></tr> <tr><td>data</td> <td>返回参数信息</td></tr></tbody></table> <p><strong>data参数解释:</strong> 存储的分页信息</p> <table><thead><tr><th>名称</th> <th>说明</th></tr></thead> <tbody><tr><td>pageNum</td> <td>当前页码</td></tr> <tr><td>pageSize</td> <td>每页条数</td></tr> <tr><td>total</td> <td>数据总数</td></tr> <tr><td>list</td> <td>数据集合</td></tr></tbody></table> <p><strong>list参数解释:</strong> 存储的是员工对象集合</p> <table><thead><tr><th>名称</th> <th>说明</th></tr></thead> <tbody><tr><td>id</td> <td>员工主键ID</td></tr> <tr><td>name</td> <td>员工名称</td></tr> <tr><td>password</td> <td>员工密码</td></tr> <tr><td>email</td> <td>员工邮箱</td></tr> <tr><td>age</td> <td>员工年龄</td></tr> <tr><td>admin</td> <td>是否管理员</td></tr> <tr><td>department.id</td> <td>部门ID</td></tr> <tr><td>department.sn</td> <td>部门编号</td></tr> <tr><td>department.name</td> <td>部门名称</td></tr></tbody></table> <p><strong>F) 代码实现</strong></p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token annotation punctuation">@GetMapping</span>
<span class="token keyword">public</span> <span class="token class-name">R</span> <span class="token function">query</span><span class="token punctuation">(</span><span class="token class-name">EmployeeQueryObject</span> qo<span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token class-name">PageInfo</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Employee</span><span class="token punctuation">&gt;</span></span> pageInfo <span class="token operator">=</span> employeeService<span class="token punctuation">.</span><span class="token function">selectList</span><span class="token punctuation">(</span>qo<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">return</span> <span class="token class-name">R</span><span class="token punctuation">.</span><span class="token function">ok</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">PageData</span><span class="token punctuation">(</span>pageInfo<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><p><strong>G) 测试</strong></p> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20240122130938363.png" alt=""></strong></p> <h3 id="_7-2-员工新增"><a href="#_7-2-员工新增" class="header-anchor">#</a> 7.2 员工新增</h3> <p><strong>A) 前端界面</strong></p> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20240122140457592.png" alt=""></strong></p> <p><strong>B) 接口说明</strong></p> <p>员工新增，需要将角色的关系一并保存</p> <p><strong>C) 请求路径</strong></p> <div class="language- line-numbers-mode"><pre class="language-text"><code>POST /api/employees
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p><strong>D) 请求参数</strong></p> <div class="language-json line-numbers-mode"><pre class="language-json"><code><span class="token punctuation">{</span>
    <span class="token property">&quot;employee&quot;</span><span class="token operator">:</span> <span class="token punctuation">{</span>
        <span class="token property">&quot;name&quot;</span><span class="token operator">:</span> <span class="token string">&quot;张龙&quot;</span><span class="token punctuation">,</span>
        <span class="token property">&quot;password&quot;</span><span class="token operator">:</span> <span class="token string">&quot;123&quot;</span><span class="token punctuation">,</span>
        <span class="token property">&quot;email&quot;</span><span class="token operator">:</span> <span class="token string">&quot;zhanglong@wolfcode.cn&quot;</span><span class="token punctuation">,</span>
        <span class="token property">&quot;age&quot;</span><span class="token operator">:</span> <span class="token string">&quot;23&quot;</span><span class="token punctuation">,</span>
        <span class="token property">&quot;department&quot;</span><span class="token operator">:</span> <span class="token punctuation">{</span>
            <span class="token property">&quot;id&quot;</span><span class="token operator">:</span> <span class="token number">5</span>
        <span class="token punctuation">}</span><span class="token punctuation">,</span>
        <span class="token property">&quot;admin&quot;</span><span class="token operator">:</span> <span class="token boolean">false</span>
    <span class="token punctuation">}</span><span class="token punctuation">,</span>
    <span class="token property">&quot;roleIds&quot;</span><span class="token operator">:</span> <span class="token punctuation">[</span>
        <span class="token number">14</span>
    <span class="token punctuation">]</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br></div></div><table><thead><tr><th>名称</th> <th>类型</th> <th>说明</th></tr></thead> <tbody><tr><td>employee.name</td> <td>string</td> <td>员工名称</td></tr> <tr><td>employee.password</td> <td>string</td> <td>员工密码</td></tr> <tr><td>employee.email</td> <td>string</td> <td>员工邮箱</td></tr> <tr><td>employee.age</td> <td>integer</td> <td>员工年龄</td></tr> <tr><td>demployee.department.id</td> <td>long</td> <td>部门id</td></tr> <tr><td>employee.admin</td> <td>boolean</td> <td>是否管理员</td></tr> <tr><td>roleIds</td> <td>array</td> <td>角色ID集合</td></tr></tbody></table> <p><strong>E) 响应格式</strong></p> <div class="language-json line-numbers-mode"><pre class="language-json"><code><span class="token punctuation">{</span>
    <span class="token property">&quot;code&quot;</span><span class="token operator">:</span> <span class="token number">200</span><span class="token punctuation">,</span>
    <span class="token property">&quot;msg&quot;</span><span class="token operator">:</span> <span class="token string">&quot;操作成功&quot;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><table><thead><tr><th>名称</th> <th>说明</th></tr></thead> <tbody><tr><td>code</td> <td>200成功，401认证失败，403无权限操作，500失败</td></tr> <tr><td>msg</td> <td>操作提示信息</td></tr></tbody></table> <p><strong>F) 代码实现</strong></p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token annotation punctuation">@PostMapping</span>
<span class="token keyword">public</span> <span class="token class-name">R</span> <span class="token function">save</span><span class="token punctuation">(</span><span class="token annotation punctuation">@RequestBody</span> <span class="token class-name">EmployeeVo</span> employeeVo<span class="token punctuation">)</span><span class="token punctuation">{</span>
    employeeService<span class="token punctuation">.</span><span class="token function">save</span><span class="token punctuation">(</span>employeeVo<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">return</span> <span class="token class-name">R</span><span class="token punctuation">.</span><span class="token function">ok</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token annotation punctuation">@Transactional</span>
<span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">save</span><span class="token punctuation">(</span><span class="token class-name">EmployeeVo</span> employeeVo<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">if</span><span class="token punctuation">(</span>employeeVo <span class="token operator">==</span><span class="token keyword">null</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
        <span class="token keyword">throw</span> <span class="token keyword">new</span> <span class="token class-name">BusinessException</span><span class="token punctuation">(</span><span class="token string">&quot;非法操作&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token class-name">Employee</span> employee <span class="token operator">=</span> employeeVo<span class="token punctuation">.</span><span class="token function">getEmployee</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token class-name">Long</span><span class="token punctuation">[</span><span class="token punctuation">]</span> roleIds <span class="token operator">=</span> employeeVo<span class="token punctuation">.</span><span class="token function">getRoleIds</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">if</span><span class="token punctuation">(</span>employee<span class="token operator">==</span><span class="token keyword">null</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
        <span class="token keyword">throw</span> <span class="token keyword">new</span> <span class="token class-name">BusinessException</span><span class="token punctuation">(</span><span class="token string">&quot;非法操作&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    employeeMapper<span class="token punctuation">.</span><span class="token function">insert</span><span class="token punctuation">(</span>employee<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment">//插入关系</span>
    <span class="token keyword">if</span><span class="token punctuation">(</span>roleIds<span class="token operator">!=</span><span class="token keyword">null</span> <span class="token operator">&amp;&amp;</span> roleIds<span class="token punctuation">.</span>length<span class="token operator">&gt;</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
        employeeMapper<span class="token punctuation">.</span><span class="token function">insertRelation</span><span class="token punctuation">(</span>employee<span class="token punctuation">.</span><span class="token function">getId</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span>roleIds<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br></div></div><p><strong>G) 测试</strong></p> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20240122141927457.png" alt=""></strong></p> <h3 id="_7-3-员工更新"><a href="#_7-3-员工更新" class="header-anchor">#</a> 7.3 员工更新</h3> <p><strong>A) 前端界面</strong></p> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20240122141957586.png" alt=""></strong></p> <p><strong>B) 接口说明</strong></p> <p>更新员工，需要将之前的角色关系删除，然后再把新关系插入</p> <p><strong>C) 请求路径</strong></p> <div class="language- line-numbers-mode"><pre class="language-text"><code>PUT /api/employees/{id}
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p><strong>D) 请求参数</strong></p> <div class="language-json line-numbers-mode"><pre class="language-json"><code><span class="token punctuation">{</span>
    <span class="token property">&quot;employee&quot;</span><span class="token operator">:</span> <span class="token punctuation">{</span>
        <span class="token property">&quot;name&quot;</span><span class="token operator">:</span> <span class="token string">&quot;张龙&quot;</span><span class="token punctuation">,</span>
        <span class="token property">&quot;password&quot;</span><span class="token operator">:</span> <span class="token string">&quot;123&quot;</span><span class="token punctuation">,</span>
        <span class="token property">&quot;email&quot;</span><span class="token operator">:</span> <span class="token string">&quot;zhanglong@wolfcode.cn&quot;</span><span class="token punctuation">,</span>
        <span class="token property">&quot;age&quot;</span><span class="token operator">:</span> <span class="token string">&quot;24&quot;</span><span class="token punctuation">,</span>
        <span class="token property">&quot;department&quot;</span><span class="token operator">:</span> <span class="token punctuation">{</span>
            <span class="token property">&quot;id&quot;</span><span class="token operator">:</span> <span class="token number">5</span>
        <span class="token punctuation">}</span><span class="token punctuation">,</span>
        <span class="token property">&quot;admin&quot;</span><span class="token operator">:</span> <span class="token boolean">false</span>
    <span class="token punctuation">}</span><span class="token punctuation">,</span>
    <span class="token property">&quot;roleIds&quot;</span><span class="token operator">:</span> <span class="token punctuation">[</span>
        <span class="token number">12</span><span class="token punctuation">,</span><span class="token number">14</span>
    <span class="token punctuation">]</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br></div></div><table><thead><tr><th>名称</th> <th>类型</th> <th>说明</th></tr></thead> <tbody><tr><td>id</td> <td>long</td> <td>员工主键</td></tr> <tr><td>employee.name</td> <td>string</td> <td>员工名称</td></tr> <tr><td>employee.password</td> <td>string</td> <td>员工密码</td></tr> <tr><td>employee.email</td> <td>string</td> <td>员工邮箱</td></tr> <tr><td>employee.age</td> <td>integer</td> <td>员工年龄</td></tr> <tr><td>demployee.department.id</td> <td>long</td> <td>部门id</td></tr> <tr><td>employee.admin</td> <td>boolean</td> <td>是否管理员</td></tr> <tr><td>roleIds</td> <td>array</td> <td>角色ID集合</td></tr></tbody></table> <p><strong>E) 响应格式</strong></p> <div class="language-json line-numbers-mode"><pre class="language-json"><code><span class="token punctuation">{</span>
    <span class="token property">&quot;code&quot;</span><span class="token operator">:</span> <span class="token number">200</span><span class="token punctuation">,</span>
    <span class="token property">&quot;msg&quot;</span><span class="token operator">:</span> <span class="token string">&quot;操作成功&quot;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><table><thead><tr><th>名称</th> <th>说明</th></tr></thead> <tbody><tr><td>code</td> <td>200成功，401认证失败，403无权限操作，500失败</td></tr> <tr><td>msg</td> <td>操作提示信息</td></tr></tbody></table> <p><strong>F) 代码实现</strong></p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token annotation punctuation">@PutMapping</span><span class="token punctuation">(</span><span class="token string">&quot;/{id}&quot;</span><span class="token punctuation">)</span>
<span class="token keyword">public</span> <span class="token class-name">R</span> <span class="token function">update</span><span class="token punctuation">(</span><span class="token annotation punctuation">@PathVariable</span><span class="token punctuation">(</span><span class="token string">&quot;id&quot;</span><span class="token punctuation">)</span> <span class="token class-name">Long</span> id<span class="token punctuation">,</span><span class="token annotation punctuation">@RequestBody</span> <span class="token class-name">EmployeeVo</span> employeeVo<span class="token punctuation">)</span><span class="token punctuation">{</span>
    employeeService<span class="token punctuation">.</span><span class="token function">update</span><span class="token punctuation">(</span>id<span class="token punctuation">,</span>employeeVo<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">return</span> <span class="token class-name">R</span><span class="token punctuation">.</span><span class="token function">ok</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token annotation punctuation">@Override</span>
<span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">update</span><span class="token punctuation">(</span><span class="token class-name">Long</span> id<span class="token punctuation">,</span><span class="token class-name">EmployeeVo</span> employeeVo<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">if</span><span class="token punctuation">(</span>id<span class="token operator">==</span><span class="token keyword">null</span> <span class="token operator">||</span> employeeVo <span class="token operator">==</span><span class="token keyword">null</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
        <span class="token keyword">throw</span> <span class="token keyword">new</span> <span class="token class-name">BusinessException</span><span class="token punctuation">(</span><span class="token string">&quot;非法操作&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token class-name">Employee</span> employee <span class="token operator">=</span> employeeVo<span class="token punctuation">.</span><span class="token function">getEmployee</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token class-name">Long</span><span class="token punctuation">[</span><span class="token punctuation">]</span> roleIds <span class="token operator">=</span> employeeVo<span class="token punctuation">.</span><span class="token function">getRoleIds</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">if</span><span class="token punctuation">(</span>employee<span class="token operator">==</span><span class="token keyword">null</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
        <span class="token keyword">throw</span> <span class="token keyword">new</span> <span class="token class-name">BusinessException</span><span class="token punctuation">(</span><span class="token string">&quot;非法操作&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    employee<span class="token punctuation">.</span><span class="token function">setId</span><span class="token punctuation">(</span>id<span class="token punctuation">)</span><span class="token punctuation">;</span>
    employeeMapper<span class="token punctuation">.</span><span class="token function">updateByPrimaryKey</span><span class="token punctuation">(</span>employee<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment">//删除旧关系</span>
    employeeMapper<span class="token punctuation">.</span><span class="token function">deleteRelationByEmpId</span><span class="token punctuation">(</span>employee<span class="token punctuation">.</span><span class="token function">getId</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">if</span><span class="token punctuation">(</span>roleIds<span class="token operator">!=</span><span class="token keyword">null</span> <span class="token operator">&amp;&amp;</span> roleIds<span class="token punctuation">.</span>length<span class="token operator">&gt;</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
        <span class="token comment">//插入关系</span>
        employeeMapper<span class="token punctuation">.</span><span class="token function">insertRelation</span><span class="token punctuation">(</span>employee<span class="token punctuation">.</span><span class="token function">getId</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span>roleIds<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br></div></div><p><strong>G) 测试</strong></p> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20240122150209554.png" alt=""></strong></p> <h3 id="_7-4-员工删除"><a href="#_7-4-员工删除" class="header-anchor">#</a> 7.4 员工删除</h3> <p><strong>A) 前端界面</strong></p> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20240122142444071.png" alt=""></strong></p> <p><strong>B) 接口说明</strong></p> <p>删除员工，需要把角色的关系删除掉</p> <p><strong>C) 请求路径</strong></p> <div class="language- line-numbers-mode"><pre class="language-text"><code>DELETE /api/employees/{id}
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p><strong>D) 请求参数</strong></p> <table><thead><tr><th>名称</th> <th>类型</th> <th>说明</th></tr></thead> <tbody><tr><td>id</td> <td>long</td> <td>员工ID</td></tr></tbody></table> <p><strong>E) 响应格式</strong></p> <div class="language-json line-numbers-mode"><pre class="language-json"><code><span class="token punctuation">{</span>
    <span class="token property">&quot;code&quot;</span><span class="token operator">:</span> <span class="token number">200</span><span class="token punctuation">,</span>
    <span class="token property">&quot;msg&quot;</span><span class="token operator">:</span> <span class="token string">&quot;操作成功&quot;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><table><thead><tr><th>名称</th> <th>说明</th></tr></thead> <tbody><tr><td>code</td> <td>200成功，401认证失败，403无权限操作，500失败</td></tr> <tr><td>msg</td> <td>操作提示信息</td></tr></tbody></table> <p><strong>F) 代码实现</strong></p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token annotation punctuation">@DeleteMapping</span><span class="token punctuation">(</span><span class="token string">&quot;/{id}&quot;</span><span class="token punctuation">)</span>
<span class="token keyword">public</span> <span class="token class-name">R</span> <span class="token function">delete</span><span class="token punctuation">(</span><span class="token annotation punctuation">@PathVariable</span><span class="token punctuation">(</span><span class="token string">&quot;id&quot;</span><span class="token punctuation">)</span> <span class="token class-name">Long</span> id<span class="token punctuation">)</span><span class="token punctuation">{</span>
    employeeService<span class="token punctuation">.</span><span class="token function">delete</span><span class="token punctuation">(</span>id<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">return</span> <span class="token class-name">R</span><span class="token punctuation">.</span><span class="token function">ok</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token annotation punctuation">@Override</span>
<span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">delete</span><span class="token punctuation">(</span><span class="token class-name">Long</span> id<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">int</span> count <span class="token operator">=</span> employeeMapper<span class="token punctuation">.</span><span class="token function">deleteByPrimaryKey</span><span class="token punctuation">(</span>id<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">if</span><span class="token punctuation">(</span>count<span class="token operator">&gt;</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
        employeeMapper<span class="token punctuation">.</span><span class="token function">deleteRelationByEmpId</span><span class="token punctuation">(</span>id<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><p><strong>G) 测试</strong></p> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20240122142621490.png" alt=""></strong></p> <h3 id="_7-5-更新管理员状态"><a href="#_7-5-更新管理员状态" class="header-anchor">#</a> 7.5 更新管理员状态</h3> <p><strong>A) 前端界面</strong></p> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20240122142702230.png" alt=""></strong></p> <p><strong>B) 接口说明</strong></p> <p>根据员工ID更新管理员状态</p> <p><strong>C) 请求路径</strong></p> <div class="language- line-numbers-mode"><pre class="language-text"><code>PATCH /api/employees/3
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p><strong>D) 请求参数</strong></p> <div class="language-json line-numbers-mode"><pre class="language-json"><code>admin=<span class="token boolean">true</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><table><thead><tr><th>名称</th> <th>类型</th> <th>说明</th></tr></thead> <tbody><tr><td>admin</td> <td>boolean</td> <td>是否管理员</td></tr></tbody></table> <p><strong>E) 响应格式</strong></p> <div class="language-json line-numbers-mode"><pre class="language-json"><code><span class="token punctuation">{</span>
    <span class="token property">&quot;code&quot;</span><span class="token operator">:</span> <span class="token number">200</span><span class="token punctuation">,</span>
    <span class="token property">&quot;msg&quot;</span><span class="token operator">:</span> <span class="token string">&quot;操作成功&quot;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><table><thead><tr><th>名称</th> <th>说明</th></tr></thead> <tbody><tr><td>code</td> <td>200成功，401认证失败，403无权限操作，500失败</td></tr> <tr><td>msg</td> <td>操作提示信息</td></tr></tbody></table> <p><strong>F) 代码实现</strong></p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token annotation punctuation">@PatchMapping</span><span class="token punctuation">(</span><span class="token string">&quot;/{id}&quot;</span><span class="token punctuation">)</span>
<span class="token keyword">public</span> <span class="token class-name">R</span> <span class="token function">updateState</span><span class="token punctuation">(</span><span class="token annotation punctuation">@PathVariable</span><span class="token punctuation">(</span><span class="token string">&quot;id&quot;</span><span class="token punctuation">)</span> <span class="token class-name">Long</span> id<span class="token punctuation">,</span><span class="token class-name">Boolean</span> admin<span class="token punctuation">)</span><span class="token punctuation">{</span>
    employeeService<span class="token punctuation">.</span><span class="token function">updateAdminState</span><span class="token punctuation">(</span>id<span class="token punctuation">,</span>admin<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">return</span> <span class="token class-name">R</span><span class="token punctuation">.</span><span class="token function">ok</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><p><strong>G) 测试</strong></p> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20240122144554321.png" alt=""></strong></p> <h3 id="_7-6-角色删除-维护关系"><a href="#_7-6-角色删除-维护关系" class="header-anchor">#</a> 7.6 角色删除(维护关系)</h3> <p>我们需要在角色删除的逻辑中，把员工和角色的关系删除掉.</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token annotation punctuation">@Transactional</span>
<span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">delete</span><span class="token punctuation">(</span><span class="token class-name">Long</span> id<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">if</span><span class="token punctuation">(</span>id<span class="token operator">==</span><span class="token keyword">null</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
        <span class="token keyword">throw</span> <span class="token keyword">new</span> <span class="token class-name">BusinessException</span><span class="token punctuation">(</span><span class="token string">&quot;非法操作&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">int</span> count <span class="token operator">=</span> roleMapper<span class="token punctuation">.</span><span class="token function">deleteByPrimaryKey</span><span class="token punctuation">(</span>id<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">if</span><span class="token punctuation">(</span>count<span class="token operator">&gt;</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
        roleMapper<span class="token punctuation">.</span><span class="token function">deletePermissionRelationByRoleId</span><span class="token punctuation">(</span>id<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">//删除员工和角色的关系</span>
        roleMapper<span class="token punctuation">.</span><span class="token function">deleteEmployeeRelationByRoleId</span><span class="token punctuation">(</span>id<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br></div></div><h2 id="八、认证管理"><a href="#八、认证管理" class="header-anchor">#</a> 八、认证管理</h2> <h3 id="_8-1-验证码获取"><a href="#_8-1-验证码获取" class="header-anchor">#</a> 8.1 验证码获取</h3> <p><strong>A) 前端界面</strong></p> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20240122150621439.png" alt=""></strong></p> <p><strong>B) 接口说明</strong></p> <p>获取验证码接口</p> <p><strong>C) 请求路径</strong></p> <div class="language- line-numbers-mode"><pre class="language-text"><code>GET /api/verifyCodes
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p><strong>D) 请求参数</strong></p> <p>无</p> <p><strong>E) 响应格式</strong></p> <div class="language-json line-numbers-mode"><pre class="language-json"><code><span class="token punctuation">{</span>
    <span class="token property">&quot;code&quot;</span><span class="token operator">:</span> <span class="token number">200</span><span class="token punctuation">,</span>
    <span class="token property">&quot;msg&quot;</span><span class="token operator">:</span> <span class="token string">&quot;操作成功&quot;</span><span class="token punctuation">,</span>
    <span class="token property">&quot;data&quot;</span><span class="token operator">:</span> <span class="token punctuation">{</span>
        <span class="token property">&quot;uuid&quot;</span><span class="token operator">:</span> <span class="token string">&quot;3162de6bf512463cbad61e4c517f53c3&quot;</span><span class="token punctuation">,</span>
        <span class="token property">&quot;img&quot;</span><span class="token operator">:</span> <span class="token string">&quot;/9j/4AAQSkZJRgABAgAAAQABAAD/2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBkSEw8UHRofHh0aHBwgJC4nICIsIxwcKDcpLDAxNDQ0Hyc5PTgyPC4zNDL/2wBDAQkJCQwLDBgNDRgyIRwhMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjL/wAARCAAUADwDASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEAAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSExBhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYkNOEl8RcYGRomJygpKjU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6goOEhYaHiImKkpOUlZaXmJmaoqOkpaanqKmqsrO0tba3uLm6wsPExcbHyMnK0tPU1dbX2Nna4uPk5ebn6Onq8vP09fb3+Pn6/9oADAMBAAIRAxEAPwD0XUtXtLRDFeGKSOUurQujBjGFII2kENlhjnaCD1OOZrS7l1Kxgla3mhExDHBUhVyeDu6ghQOmfnGPUZvilFt9Onk2IzXBVDI7fMuCpCr8v3flJOSOfXNX7Z447O2068SFleJLYxk790nllnRlxgDYM8nnP0yK7Ha5laxJd6rrT6NbyOtsEU3ChFIPIOTkjcvKgrnkZGCDg3NMu7h9Vu9Kv2jnkh2yrIIwNw4IJ9CMpjjseelUo5pbfxLf3s2m3ro37pBFAXDYI+bJx/dB/H2q5oFjOLq81K8ikjnuHOxHYkqhw2M/kMY42/hRpYlq+4smpxWEvkI01yd/7iOBzKzhV2MrliTkMrZ6c+pDVbsdYstWiKwyMpY7GRiUYZB6Ed8KTwegrBsbmMa5q93d+cGjXzFQRushRWBHTGBgJkEcg88bsz6ddRz67q2sR7jaxQ46YZsAdB/wA9fUUtLuw9n2JLfX57zUprL7OsiNI4tZY5GRWZPmAZhng4GcA9ehBpkOr61eyXEum2lvPbCZlV2JGcYxwWGOMdAKz7G2kh8OR38TYuLeY3MZOQpThGUngc7emSccd62/CUHlaEr7s+dIz4x0/hx/47+tPRju9hYbdL7U9VtrgBxE6mNii5BdF5IxtYjAAJBIHGeTWrFahbdYZ5GucNv3zBSSd24HgAcHGOOMCiirmknoUtYNvy/Ug0q6kvbWWaXbuFzPEAowAqSsg/RQT7k/So9Ru30yC1WABgzMn7wljhYncck5Jyg5PvRRWbejM5PR/MyvEMFpHe2atZxSmZxvd2bd808MZ5BGeJGxnpgY44ra07TINMjdIWkbeQzFiME4wSAAAM9eAPyAAKK1svZ38zSWyZZeCNwRgrudXYoxUkjGMkYz0Ax3HB44pLWVprSGVgAzorHHTJGaKKyb95E9D//Z&quot;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><table><thead><tr><th>名称</th> <th>说明</th></tr></thead> <tbody><tr><td>code</td> <td>200成功，401认证失败，403无权限操作，500失败</td></tr> <tr><td>msg</td> <td>操作提示信息</td></tr> <tr><td>data</td> <td>返回的数据</td></tr></tbody></table> <p>data数据解释:</p> <table><thead><tr><th>名称</th> <th>说明</th></tr></thead> <tbody><tr><td>uuid</td> <td>唯一标识，用于后台进行验证使用</td></tr> <tr><td>img</td> <td>图片Base64格式</td></tr></tbody></table> <p><strong>F) 代码实现</strong></p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token annotation punctuation">@GetMapping</span>
<span class="token keyword">public</span> <span class="token class-name">R</span> <span class="token function">getVerifyCode</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token class-name">VerifyCodeVo</span> verifyCode <span class="token operator">=</span> verifyCodeSerivice<span class="token punctuation">.</span><span class="token function">getVerifyCode</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">return</span> <span class="token class-name">R</span><span class="token punctuation">.</span><span class="token function">ok</span><span class="token punctuation">(</span>verifyCode<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token annotation punctuation">@Override</span>
<span class="token keyword">public</span> <span class="token class-name">VerifyCodeVo</span> <span class="token function">getVerifyCode</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token class-name">String</span> randomCode <span class="token operator">=</span> <span class="token class-name">VerifyCodeUtil</span><span class="token punctuation">.</span><span class="token function">generateRandomCode</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token class-name">String</span> img <span class="token operator">=</span> <span class="token class-name">VerifyCodeUtil</span><span class="token punctuation">.</span><span class="token function">generateVerifyCode</span><span class="token punctuation">(</span>randomCode<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token class-name">String</span> uuid <span class="token operator">=</span> <span class="token constant">UUID</span><span class="token punctuation">.</span><span class="token function">randomUUID</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">toString</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">replace</span><span class="token punctuation">(</span><span class="token string">&quot;-&quot;</span><span class="token punctuation">,</span><span class="token string">&quot;&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token class-name">String</span> key <span class="token operator">=</span> <span class="token class-name">RedisConstants</span><span class="token punctuation">.</span><span class="token constant">VERIFY_CODE_PREFIX</span><span class="token operator">+</span>uuid<span class="token punctuation">;</span>
    redisTemplate<span class="token punctuation">.</span><span class="token function">opsForValue</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">set</span><span class="token punctuation">(</span>key<span class="token punctuation">,</span>randomCode<span class="token punctuation">,</span><span class="token class-name">RedisConstants</span><span class="token punctuation">.</span><span class="token constant">VERIFY_CODE_EXPIRE</span><span class="token punctuation">,</span> <span class="token class-name">TimeUnit</span><span class="token punctuation">.</span><span class="token constant">SECONDS</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">return</span> <span class="token keyword">new</span> <span class="token class-name">VerifyCodeVo</span><span class="token punctuation">(</span>uuid<span class="token punctuation">,</span>img<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div><p><strong>G) 测试</strong></p> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20240122151149814.png" alt=""></strong></p> <h3 id="_8-2-登录功能"><a href="#_8-2-登录功能" class="header-anchor">#</a> 8.2 登录功能</h3> <p><strong>A) 前端页面</strong></p> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20240122151506584.png" alt=""></strong></p> <p><strong>B) 接口说明</strong></p> <p>登录接口，返回token信息</p> <p><strong>C) 请求路径</strong></p> <div class="language- line-numbers-mode"><pre class="language-text"><code>POST /api/tokens
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p><strong>D) 请求参数</strong></p> <div class="language-json line-numbers-mode"><pre class="language-json"><code><span class="token punctuation">{</span>
    <span class="token property">&quot;uuid&quot;</span><span class="token operator">:</span> <span class="token string">&quot;6dffdb436dc54e4dbdd78ae2cb54cef1&quot;</span><span class="token punctuation">,</span>
    <span class="token property">&quot;name&quot;</span><span class="token operator">:</span> <span class="token string">&quot;admin&quot;</span><span class="token punctuation">,</span>
    <span class="token property">&quot;password&quot;</span><span class="token operator">:</span> <span class="token string">&quot;123&quot;</span><span class="token punctuation">,</span>
    <span class="token property">&quot;code&quot;</span><span class="token operator">:</span> <span class="token string">&quot;BOJU&quot;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><table><thead><tr><th>名称</th> <th>类型</th> <th>说明</th></tr></thead> <tbody><tr><td>uuid</td> <td>string</td> <td>唯一表示</td></tr> <tr><td>name</td> <td>string</td> <td>用户名</td></tr> <tr><td>password</td> <td>string</td> <td>用户密码</td></tr> <tr><td>code</td> <td>string</td> <td>验证码</td></tr></tbody></table> <p><strong>E) 响应格式</strong></p> <div class="language-json line-numbers-mode"><pre class="language-json"><code><span class="token punctuation">{</span>
    <span class="token property">&quot;code&quot;</span><span class="token operator">:</span> <span class="token number">200</span><span class="token punctuation">,</span>
    <span class="token property">&quot;msg&quot;</span><span class="token operator">:</span> <span class="token string">&quot;操作成功&quot;</span><span class="token punctuation">,</span>
    <span class="token property">&quot;data&quot;</span><span class="token operator">:</span> <span class="token string">&quot;f9de939832ef4e16b3de8690b207059e&quot;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><table><thead><tr><th>名称</th> <th>说明</th></tr></thead> <tbody><tr><td>code</td> <td>200成功，401认证失败，403无权限操作，500失败</td></tr> <tr><td>msg</td> <td>操作提示信息</td></tr> <tr><td>data</td> <td>返回的token</td></tr></tbody></table> <p><strong>F) 代码实现</strong></p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token annotation punctuation">@PostMapping</span>
<span class="token keyword">public</span> <span class="token class-name">R</span> <span class="token function">login</span><span class="token punctuation">(</span><span class="token annotation punctuation">@RequestBody</span> <span class="token class-name">LoginVo</span> vo<span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token class-name">String</span> token <span class="token operator">=</span> tokenService<span class="token punctuation">.</span><span class="token function">login</span><span class="token punctuation">(</span>vo<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">return</span> <span class="token class-name">R</span><span class="token punctuation">.</span><span class="token function">ok</span><span class="token punctuation">(</span>token<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token annotation punctuation">@Override</span>
<span class="token keyword">public</span> <span class="token class-name">String</span> <span class="token function">login</span><span class="token punctuation">(</span><span class="token class-name">LoginVo</span> vo<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">if</span><span class="token punctuation">(</span>vo<span class="token operator">==</span><span class="token keyword">null</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
        <span class="token keyword">throw</span> <span class="token keyword">new</span> <span class="token class-name">BusinessException</span><span class="token punctuation">(</span><span class="token string">&quot;非法操作&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token class-name">StringUtils</span><span class="token punctuation">.</span><span class="token function">isEmpty</span><span class="token punctuation">(</span>vo<span class="token punctuation">.</span><span class="token function">getName</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">||</span> <span class="token class-name">StringUtils</span><span class="token punctuation">.</span><span class="token function">isEmpty</span><span class="token punctuation">(</span>vo<span class="token punctuation">.</span><span class="token function">getPassword</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
        <span class="token keyword">throw</span> <span class="token keyword">new</span> <span class="token class-name">BusinessException</span><span class="token punctuation">(</span><span class="token string">&quot;账号密码不能为空&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token class-name">String</span> code <span class="token operator">=</span> vo<span class="token punctuation">.</span><span class="token function">getCode</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token class-name">StringUtils</span><span class="token punctuation">.</span><span class="token function">isEmpty</span><span class="token punctuation">(</span>code<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
        <span class="token keyword">throw</span> <span class="token keyword">new</span> <span class="token class-name">BusinessException</span><span class="token punctuation">(</span><span class="token string">&quot;验证码不能为空&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token class-name">String</span> key <span class="token operator">=</span> <span class="token class-name">RedisConstants</span><span class="token punctuation">.</span><span class="token constant">VERIFY_CODE_PREFIX</span><span class="token operator">+</span>vo<span class="token punctuation">.</span><span class="token function">getUuid</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token class-name">String</span> redisCode <span class="token operator">=</span> redisTemplate<span class="token punctuation">.</span><span class="token function">opsForValue</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span>key<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token operator">!</span><span class="token class-name">VerifyCodeUtil</span><span class="token punctuation">.</span><span class="token function">verification</span><span class="token punctuation">(</span>redisCode<span class="token punctuation">,</span>vo<span class="token punctuation">.</span><span class="token function">getCode</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
        <span class="token keyword">throw</span> <span class="token keyword">new</span> <span class="token class-name">BusinessException</span><span class="token punctuation">(</span><span class="token string">&quot;验证码错误&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token class-name">Employee</span> employee <span class="token operator">=</span> employeeService<span class="token punctuation">.</span><span class="token function">getByNameAndPassword</span><span class="token punctuation">(</span>vo<span class="token punctuation">.</span><span class="token function">getName</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span>vo<span class="token punctuation">.</span><span class="token function">getPassword</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">if</span><span class="token punctuation">(</span>employee<span class="token operator">==</span><span class="token keyword">null</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
        <span class="token keyword">throw</span> <span class="token keyword">new</span> <span class="token class-name">BusinessException</span><span class="token punctuation">(</span><span class="token string">&quot;账号和密码不匹配&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token class-name">String</span> token  <span class="token operator">=</span> <span class="token function">generateToken</span><span class="token punctuation">(</span>employee<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">return</span> token<span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token keyword">private</span> <span class="token class-name">String</span> <span class="token function">generateToken</span><span class="token punctuation">(</span><span class="token class-name">Employee</span> employee<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token class-name">String</span> token <span class="token operator">=</span> <span class="token constant">UUID</span><span class="token punctuation">.</span><span class="token function">randomUUID</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">toString</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">replace</span><span class="token punctuation">(</span><span class="token string">&quot;-&quot;</span><span class="token punctuation">,</span><span class="token string">&quot;&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token class-name">String</span> tokenKey <span class="token operator">=</span> <span class="token class-name">RedisConstants</span><span class="token punctuation">.</span><span class="token constant">TOKEN_PREFIX</span><span class="token operator">+</span>token<span class="token punctuation">;</span>
    redisTemplate<span class="token punctuation">.</span><span class="token function">opsForValue</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">set</span><span class="token punctuation">(</span>tokenKey<span class="token punctuation">,</span><span class="token constant">JSON</span><span class="token punctuation">.</span><span class="token function">toJSONString</span><span class="token punctuation">(</span>employee<span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token class-name">RedisConstants</span><span class="token punctuation">.</span><span class="token constant">TOKEN_EXPIRE</span><span class="token punctuation">,</span> <span class="token class-name">TimeUnit</span><span class="token punctuation">.</span><span class="token constant">SECONDS</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token class-name">List</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">String</span><span class="token punctuation">&gt;</span></span> permissions <span class="token operator">=</span> permissionService<span class="token punctuation">.</span><span class="token function">selectExpressionsByEmpId</span><span class="token punctuation">(</span>employee<span class="token punctuation">.</span><span class="token function">getId</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token class-name">String</span> permissionKey <span class="token operator">=</span> <span class="token class-name">RedisConstants</span><span class="token punctuation">.</span><span class="token constant">USER_PERMISSION_PREFIX</span><span class="token operator">+</span>token<span class="token punctuation">;</span>
    redisTemplate<span class="token punctuation">.</span><span class="token function">opsForValue</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">set</span><span class="token punctuation">(</span>permissionKey<span class="token punctuation">,</span><span class="token constant">JSON</span><span class="token punctuation">.</span><span class="token function">toJSONString</span><span class="token punctuation">(</span>permissions<span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token class-name">RedisConstants</span><span class="token punctuation">.</span><span class="token constant">USER_PERMISSION_EXPIRE</span><span class="token punctuation">,</span><span class="token class-name">TimeUnit</span><span class="token punctuation">.</span><span class="token constant">SECONDS</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">return</span> token<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br></div></div><p><strong>G) 测试</strong></p> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20240122152057245.png" alt=""></strong></p> <h3 id="_8-3-登出功能"><a href="#_8-3-登出功能" class="header-anchor">#</a> 8.3 登出功能</h3> <p><strong>A) 前端页面</strong></p> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20240122152150363.png" alt=""></strong></p> <p><strong>B) 接口说明</strong></p> <p>登出，清除Token相关的信息</p> <p><strong>C) 请求路径</strong></p> <div class="language- line-numbers-mode"><pre class="language-text"><code>DELETE /api/tokens
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p><strong>D) 请求头参数</strong></p> <table><thead><tr><th>名称</th> <th>类型</th> <th>说明</th></tr></thead> <tbody><tr><td>token</td> <td>string</td> <td>认证信息</td></tr></tbody></table> <p><strong>E) 响应格式</strong></p> <div class="language-json line-numbers-mode"><pre class="language-json"><code><span class="token punctuation">{</span>
    <span class="token property">&quot;code&quot;</span><span class="token operator">:</span> <span class="token number">200</span><span class="token punctuation">,</span>
    <span class="token property">&quot;msg&quot;</span><span class="token operator">:</span> <span class="token string">&quot;操作成功&quot;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><table><thead><tr><th>名称</th> <th>说明</th></tr></thead> <tbody><tr><td>code</td> <td>200成功，401认证失败，403无权限操作，500失败</td></tr> <tr><td>msg</td> <td>操作提示信息</td></tr></tbody></table> <p><strong>F) 代码实现</strong></p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token annotation punctuation">@DeleteMapping</span>
<span class="token keyword">public</span> <span class="token class-name">R</span> <span class="token function">logout</span><span class="token punctuation">(</span><span class="token annotation punctuation">@RequestHeader</span><span class="token punctuation">(</span>name <span class="token operator">=</span> <span class="token string">&quot;token&quot;</span><span class="token punctuation">)</span> <span class="token class-name">String</span> token<span class="token punctuation">)</span><span class="token punctuation">{</span>
   tokenService<span class="token punctuation">.</span><span class="token function">logout</span><span class="token punctuation">(</span>token<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">return</span> <span class="token class-name">R</span><span class="token punctuation">.</span><span class="token function">ok</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token annotation punctuation">@Override</span>
<span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">logout</span><span class="token punctuation">(</span><span class="token class-name">String</span> token<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token class-name">String</span> tokenKey <span class="token operator">=</span> <span class="token class-name">RedisConstants</span><span class="token punctuation">.</span><span class="token constant">TOKEN_PREFIX</span><span class="token operator">+</span>token<span class="token punctuation">;</span>
    <span class="token class-name">String</span> permissionKey <span class="token operator">=</span> <span class="token class-name">RedisConstants</span><span class="token punctuation">.</span><span class="token constant">USER_PERMISSION_PREFIX</span><span class="token operator">+</span>token<span class="token punctuation">;</span>
    redisTemplate<span class="token punctuation">.</span><span class="token function">delete</span><span class="token punctuation">(</span>tokenKey<span class="token punctuation">)</span><span class="token punctuation">;</span>
    redisTemplate<span class="token punctuation">.</span><span class="token function">delete</span><span class="token punctuation">(</span>permissionKey<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><p><strong>G) 测试</strong></p> <p><strong><img src="https://cdn.jsdelivr.net/gh/langfeiyes/images-khub/image-20240122152453114.png" alt=""></strong></p> <h2 id="九、拦截管理"><a href="#九、拦截管理" class="header-anchor">#</a> 九、拦截管理</h2> <h3 id="_9-1-登录拦截"><a href="#_9-1-登录拦截" class="header-anchor">#</a> 9.1 登录拦截</h3> <p>拦截器实现:</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token annotation punctuation">@Component</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">CheckLoginInterceptor</span> <span class="token keyword">implements</span> <span class="token class-name">HandlerInterceptor</span> <span class="token punctuation">{</span>
    <span class="token annotation punctuation">@Autowired</span>
    <span class="token keyword">private</span> <span class="token class-name">StringRedisTemplate</span> redisTemplate<span class="token punctuation">;</span>
    <span class="token annotation punctuation">@Override</span>
    <span class="token keyword">public</span> <span class="token keyword">boolean</span> <span class="token function">preHandle</span><span class="token punctuation">(</span><span class="token class-name">HttpServletRequest</span> request<span class="token punctuation">,</span> <span class="token class-name">HttpServletResponse</span> response<span class="token punctuation">,</span> <span class="token class-name">Object</span> handler<span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">Exception</span> <span class="token punctuation">{</span>
        <span class="token class-name">String</span> token <span class="token operator">=</span> request<span class="token punctuation">.</span><span class="token function">getHeader</span><span class="token punctuation">(</span><span class="token string">&quot;token&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token class-name">StringUtils</span><span class="token punctuation">.</span><span class="token function">isEmpty</span><span class="token punctuation">(</span>token<span class="token punctuation">)</span> <span class="token operator">||</span> <span class="token operator">!</span>redisTemplate<span class="token punctuation">.</span><span class="token function">hasKey</span><span class="token punctuation">(</span><span class="token class-name">RedisConstants</span><span class="token punctuation">.</span><span class="token constant">TOKEN_PREFIX</span><span class="token operator">+</span>token<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
            response<span class="token punctuation">.</span><span class="token function">setContentType</span><span class="token punctuation">(</span><span class="token string">&quot;application/json;charset=utf8&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

            response<span class="token punctuation">.</span><span class="token function">getWriter</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">write</span><span class="token punctuation">(</span><span class="token constant">JSON</span><span class="token punctuation">.</span><span class="token function">toJSONString</span><span class="token punctuation">(</span><span class="token class-name">R</span><span class="token punctuation">.</span><span class="token function">fail</span><span class="token punctuation">(</span><span class="token number">401</span><span class="token punctuation">,</span><span class="token string">&quot;认证失败&quot;</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token keyword">return</span> <span class="token boolean">false</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
        <span class="token keyword">return</span> <span class="token boolean">true</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token annotation punctuation">@Override</span>
    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">postHandle</span><span class="token punctuation">(</span><span class="token class-name">HttpServletRequest</span> request<span class="token punctuation">,</span> <span class="token class-name">HttpServletResponse</span> response<span class="token punctuation">,</span> <span class="token class-name">Object</span> handler<span class="token punctuation">,</span> <span class="token class-name">ModelAndView</span> modelAndView<span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">Exception</span> <span class="token punctuation">{</span>
        <span class="token class-name">String</span> token <span class="token operator">=</span> request<span class="token punctuation">.</span><span class="token function">getHeader</span><span class="token punctuation">(</span><span class="token string">&quot;token&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">String</span> tokenKey <span class="token operator">=</span> <span class="token class-name">RedisConstants</span><span class="token punctuation">.</span><span class="token constant">TOKEN_PREFIX</span><span class="token operator">+</span>token<span class="token punctuation">;</span>
        <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token operator">!</span><span class="token class-name">StringUtils</span><span class="token punctuation">.</span><span class="token function">isEmpty</span><span class="token punctuation">(</span>token<span class="token punctuation">)</span> <span class="token operator">&amp;&amp;</span> redisTemplate<span class="token punctuation">.</span><span class="token function">hasKey</span><span class="token punctuation">(</span>tokenKey<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
            redisTemplate<span class="token punctuation">.</span><span class="token function">expire</span><span class="token punctuation">(</span>tokenKey<span class="token punctuation">,</span><span class="token class-name">RedisConstants</span><span class="token punctuation">.</span><span class="token constant">TOKEN_EXPIRE</span><span class="token punctuation">,</span> <span class="token class-name">TimeUnit</span><span class="token punctuation">.</span><span class="token constant">SECONDS</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token class-name">String</span> permissionKey <span class="token operator">=</span> <span class="token class-name">RedisConstants</span><span class="token punctuation">.</span><span class="token constant">USER_PERMISSION_PREFIX</span><span class="token operator">+</span>token<span class="token punctuation">;</span>
            redisTemplate<span class="token punctuation">.</span><span class="token function">expire</span><span class="token punctuation">(</span>permissionKey<span class="token punctuation">,</span><span class="token class-name">RedisConstants</span><span class="token punctuation">.</span><span class="token constant">USER_PERMISSION_EXPIRE</span><span class="token punctuation">,</span> <span class="token class-name">TimeUnit</span><span class="token punctuation">.</span><span class="token constant">SECONDS</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br></div></div><p>拦截器配置:</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token annotation punctuation">@Configuration</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">WebMVCConfig</span> <span class="token keyword">implements</span> <span class="token class-name">WebMvcConfigurer</span> <span class="token punctuation">{</span>
    <span class="token annotation punctuation">@Autowired</span>
    <span class="token keyword">private</span> <span class="token class-name">CheckLoginInterceptor</span> checkLoginInterceptor<span class="token punctuation">;</span>
    <span class="token annotation punctuation">@Override</span>
    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">addInterceptors</span><span class="token punctuation">(</span><span class="token class-name">InterceptorRegistry</span> registry<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        registry<span class="token punctuation">.</span><span class="token function">addInterceptor</span><span class="token punctuation">(</span>checkLoginInterceptor<span class="token punctuation">)</span>
                <span class="token punctuation">.</span><span class="token function">addPathPatterns</span><span class="token punctuation">(</span><span class="token string">&quot;/**&quot;</span><span class="token punctuation">)</span>
                <span class="token punctuation">.</span><span class="token function">excludePathPatterns</span><span class="token punctuation">(</span><span class="token string">&quot;/api/verifyCodes&quot;</span><span class="token punctuation">,</span><span class="token string">&quot;/api/tokens&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br></div></div><h3 id="_9-2-权限拦截"><a href="#_9-2-权限拦截" class="header-anchor">#</a> 9.2 权限拦截</h3> <p>拦截器实现:</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token annotation punctuation">@Component</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">CheckPermissionInterceptor</span> <span class="token keyword">implements</span> <span class="token class-name">HandlerInterceptor</span> <span class="token punctuation">{</span>
    <span class="token annotation punctuation">@Autowired</span>
    <span class="token keyword">private</span> <span class="token class-name">StringRedisTemplate</span> redisTemplate<span class="token punctuation">;</span>
    <span class="token annotation punctuation">@Override</span>
    <span class="token keyword">public</span> <span class="token keyword">boolean</span> <span class="token function">preHandle</span><span class="token punctuation">(</span><span class="token class-name">HttpServletRequest</span> request<span class="token punctuation">,</span> <span class="token class-name">HttpServletResponse</span> response<span class="token punctuation">,</span> <span class="token class-name">Object</span> handler<span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">Exception</span> <span class="token punctuation">{</span>
        <span class="token class-name">String</span> token <span class="token operator">=</span> request<span class="token punctuation">.</span><span class="token function">getHeader</span><span class="token punctuation">(</span><span class="token string">&quot;token&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">String</span> tokenKey <span class="token operator">=</span> <span class="token class-name">RedisConstants</span><span class="token punctuation">.</span><span class="token constant">TOKEN_PREFIX</span><span class="token operator">+</span>token<span class="token punctuation">;</span>
        <span class="token class-name">String</span> objJson <span class="token operator">=</span> redisTemplate<span class="token punctuation">.</span><span class="token function">opsForValue</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span>tokenKey<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">Employee</span> employee <span class="token operator">=</span> <span class="token constant">JSON</span><span class="token punctuation">.</span><span class="token function">parseObject</span><span class="token punctuation">(</span>objJson<span class="token punctuation">,</span> <span class="token class-name">Employee</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">if</span><span class="token punctuation">(</span>employee<span class="token punctuation">.</span><span class="token function">getAdmin</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
            <span class="token keyword">return</span> <span class="token boolean">true</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
        <span class="token keyword">if</span><span class="token punctuation">(</span>handler <span class="token keyword">instanceof</span> <span class="token class-name">HandlerMethod</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
            <span class="token class-name">HandlerMethod</span> hd <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token class-name">HandlerMethod</span><span class="token punctuation">)</span>handler<span class="token punctuation">;</span>
            <span class="token class-name">RequiredPermission</span> methodAnnotation <span class="token operator">=</span> hd<span class="token punctuation">.</span><span class="token function">getMethodAnnotation</span><span class="token punctuation">(</span><span class="token class-name">RequiredPermission</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token keyword">if</span><span class="token punctuation">(</span>methodAnnotation<span class="token operator">==</span><span class="token keyword">null</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
                <span class="token keyword">return</span> <span class="token boolean">true</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span>
            <span class="token class-name">String</span> permissionKey <span class="token operator">=</span> <span class="token class-name">RedisConstants</span><span class="token punctuation">.</span><span class="token constant">USER_PERMISSION_PREFIX</span><span class="token operator">+</span>token<span class="token punctuation">;</span>
            <span class="token class-name">String</span> expressionJson <span class="token operator">=</span> redisTemplate<span class="token punctuation">.</span><span class="token function">opsForValue</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span>permissionKey<span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token class-name">List</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">String</span><span class="token punctuation">&gt;</span></span> expressions <span class="token operator">=</span> <span class="token constant">JSON</span><span class="token punctuation">.</span><span class="token function">parseObject</span><span class="token punctuation">(</span>expressionJson<span class="token punctuation">,</span><span class="token class-name">List</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token keyword">if</span><span class="token punctuation">(</span>expressions<span class="token punctuation">.</span><span class="token function">contains</span><span class="token punctuation">(</span>methodAnnotation<span class="token punctuation">.</span><span class="token function">expression</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
                <span class="token keyword">return</span> <span class="token boolean">true</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span>
            response<span class="token punctuation">.</span><span class="token function">setContentType</span><span class="token punctuation">(</span><span class="token string">&quot;application/json;charset=utf8&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

            response<span class="token punctuation">.</span><span class="token function">getWriter</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">write</span><span class="token punctuation">(</span><span class="token constant">JSON</span><span class="token punctuation">.</span><span class="token function">toJSONString</span><span class="token punctuation">(</span><span class="token class-name">R</span><span class="token punctuation">.</span><span class="token function">fail</span><span class="token punctuation">(</span><span class="token number">403</span><span class="token punctuation">,</span><span class="token string">&quot;没有权限&quot;</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
        <span class="token keyword">return</span> <span class="token boolean">false</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br></div></div><p>拦截器配置:</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token annotation punctuation">@Configuration</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">WebMVCConfig</span> <span class="token keyword">implements</span> <span class="token class-name">WebMvcConfigurer</span> <span class="token punctuation">{</span>
    <span class="token annotation punctuation">@Autowired</span>
    <span class="token keyword">private</span> <span class="token class-name">CheckLoginInterceptor</span> checkLoginInterceptor<span class="token punctuation">;</span>
    <span class="token annotation punctuation">@Autowired</span>
    <span class="token keyword">private</span> <span class="token class-name">CheckPermissionInterceptor</span> checkPermissionInterceptor<span class="token punctuation">;</span>

    <span class="token annotation punctuation">@Override</span>
    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">addInterceptors</span><span class="token punctuation">(</span><span class="token class-name">InterceptorRegistry</span> registry<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        registry<span class="token punctuation">.</span><span class="token function">addInterceptor</span><span class="token punctuation">(</span>checkLoginInterceptor<span class="token punctuation">)</span>
                <span class="token punctuation">.</span><span class="token function">addPathPatterns</span><span class="token punctuation">(</span><span class="token string">&quot;/**&quot;</span><span class="token punctuation">)</span>
                <span class="token punctuation">.</span><span class="token function">excludePathPatterns</span><span class="token punctuation">(</span><span class="token string">&quot;/api/verifyCodes&quot;</span><span class="token punctuation">,</span><span class="token string">&quot;/api/tokens&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        registry<span class="token punctuation">.</span><span class="token function">addInterceptor</span><span class="token punctuation">(</span>checkPermissionInterceptor<span class="token punctuation">)</span>
                <span class="token punctuation">.</span><span class="token function">addPathPatterns</span><span class="token punctuation">(</span><span class="token string">&quot;/**&quot;</span><span class="token punctuation">)</span>
                <span class="token punctuation">.</span><span class="token function">excludePathPatterns</span><span class="token punctuation">(</span><span class="token string">&quot;/api/verifyCodes&quot;</span><span class="token punctuation">,</span><span class="token string">&quot;/api/tokens&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br></div></div><h2 id="十、项目联调"><a href="#十、项目联调" class="header-anchor">#</a> 十、项目联调</h2> <h3 id="_10-1-跨域全局配置"><a href="#_10-1-跨域全局配置" class="header-anchor">#</a> 10.1 跨域全局配置</h3> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token annotation punctuation">@Configuration</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">WebMVCConfig</span> <span class="token keyword">implements</span> <span class="token class-name">WebMvcConfigurer</span> <span class="token punctuation">{</span>
    <span class="token annotation punctuation">@Bean</span>
    <span class="token keyword">public</span> <span class="token class-name">CorsFilter</span> <span class="token function">corsFilter</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token class-name">CorsConfiguration</span> config <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">CorsConfiguration</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        config<span class="token punctuation">.</span><span class="token function">setAllowCredentials</span><span class="token punctuation">(</span><span class="token boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">// 设置访问源地址</span>
        config<span class="token punctuation">.</span><span class="token function">addAllowedOrigin</span><span class="token punctuation">(</span><span class="token string">&quot;*&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">// 设置访问源请求头</span>
        config<span class="token punctuation">.</span><span class="token function">addAllowedHeader</span><span class="token punctuation">(</span><span class="token string">&quot;*&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">// 设置访问源请求方法</span>
        config<span class="token punctuation">.</span><span class="token function">addAllowedMethod</span><span class="token punctuation">(</span><span class="token string">&quot;*&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">// 有效期 1800秒</span>
        config<span class="token punctuation">.</span><span class="token function">setMaxAge</span><span class="token punctuation">(</span><span class="token number">1800L</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">// 添加映射路径，拦截一切请求</span>
        <span class="token class-name">UrlBasedCorsConfigurationSource</span> source <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">UrlBasedCorsConfigurationSource</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        source<span class="token punctuation">.</span><span class="token function">registerCorsConfiguration</span><span class="token punctuation">(</span><span class="token string">&quot;/**&quot;</span><span class="token punctuation">,</span> config<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">// 返回新的CorsFilter</span>
        <span class="token keyword">return</span> <span class="token keyword">new</span> <span class="token class-name">CorsFilter</span><span class="token punctuation">(</span>source<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br></div></div><h2 id="十一、阅读前端代码"><a href="#十一、阅读前端代码" class="header-anchor">#</a> 十一、阅读前端代码</h2> <p>现目前外面公司的项目前后端分离的比例还是很高的， 后端人员只需要写接口，前端人员负责写界面， 但是如果作为后端人员能够看懂前端的代码肯定是更好的。</p> <blockquote><p>注意：这个章节主要的目的，想要利用这个前端项目提高一下大家看代码的能力， 因为以后进入到公司前一周不是写代码而是看代码熟悉项目，这就考验大家看代码的能力，作为一名合格的程序员我们不仅仅要会写代码还要会看代码， 还要会模仿别人的代码改造出来我们自己的代码。</p></blockquote> <h2 id="十二、小结与作业"><a href="#十二、小结与作业" class="header-anchor">#</a> 十二、小结与作业</h2> <ul><li>设计并实现RBAC权限管理系统</li></ul></div></div>  <div class="page-edit"><!----> <div class="tags"><a href="/tags/?tag=RBAC" title="标签">#RBAC</a></div> <!----></div> <div class="page-nav-wapper"><div class="page-nav-centre-wrap"><a href="/pages/6ea31e/" class="page-nav-centre page-nav-centre-prev"><div class="tooltip">RESTful课件</div></a> <a href="/pages/9927d2/" class="page-nav-centre page-nav-centre-next"><div class="tooltip">SpringSecurity</div></a></div> <div class="page-nav"><p class="inner"><span class="prev">
        ←
        <a href="/pages/6ea31e/" class="prev">RESTful课件</a></span> <span class="next"><a href="/pages/9927d2/">SpringSecurity</a>→
      </span></p></div></div></div> <div class="article-list"><div class="article-title"><a href="/archives/" class="iconfont icon-bi">最近更新</a></div> <div class="article-wrapper"><dl><dd>01</dd> <dt><a href="/pages/0c07b2/"><div>
            IT杂货铺
            <!----></div></a> <span class="date">03-23</span></dt></dl><dl><dd>02</dd> <dt><a href="/pages/705b35/"><div>
            人事百问
            <!----></div></a> <span class="date">03-22</span></dt></dl><dl><dd>03</dd> <dt><a href="/pages/78eb56/"><div>
            MyBatis-Plus
            <!----></div></a> <span class="date">03-12</span></dt></dl> <dl><dd></dd> <dt><a href="/archives/" class="more">更多文章&gt;</a></dt></dl></div></div></main></div> <div class="footer"><div class="icons"><a href="mailto:langfeiyes@163.com" title="发邮件" target="_blank" class="iconfont icon-youjian"></a><a href="https://github.com/langfeiyes" title="GitHub" target="_blank" class="iconfont icon-github"></a><a href="https://music.163.com/#/playlist?id=755597173" title="听音乐" target="_blank" class="iconfont icon-erji"></a></div> 
  Theme by
  <a href="https://github.com/xugaoyi/vuepress-theme-vdoing" target="_blank" title="本站主题">Vdoing</a> 
    | Copyright © 2024-2024
    <span><a href="https://beian.miit.gov.cn/" target="_blank">粤ICP备18007927号-1</a></span></div> <div class="buttons"><div title="返回顶部" class="button blur go-to-top iconfont icon-fanhuidingbu" style="display:none;"></div> <div title="去评论" class="button blur go-to-comment iconfont icon-pinglun" style="display:none;"></div> <div title="主题模式" class="button blur theme-mode-but iconfont icon-zhuti"><ul class="select-box" style="display:none;"><li class="iconfont icon-zidong">
          跟随系统
        </li><li class="iconfont icon-rijianmoshi">
          浅色模式
        </li><li class="iconfont icon-yejianmoshi">
          深色模式
        </li><li class="iconfont icon-yuedu">
          阅读模式
        </li></ul></div></div> <!----> <!----> <!----></div><div class="global-ui"><div></div></div></div>
    <script src="/assets/js/app.a2a00aa5.js" defer></script><script src="/assets/js/2.256f807a.js" defer></script><script src="/assets/js/58.932e3864.js" defer></script>
  </body>
</html>
